1 /*
2     Scan Tailor - Interactive post-processing tool for scanned pages.
3     Copyright (C) 2007-2009  Joseph Artsimovich <joseph_a@mail.ru>
4 
5     This program is free software: you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation, either version 3 of the License, or
8     (at your option) any later version.
9 
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14 
15     You should have received a copy of the GNU General Public License
16     along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #include "NewOpenProjectPanel.h"
20 #include <QFileInfo>
21 #include <QPainter>
22 #include "ColorSchemeManager.h"
23 #include "RecentProjects.h"
24 #include "Utils.h"
25 
NewOpenProjectPanel(QWidget * parent)26 NewOpenProjectPanel::NewOpenProjectPanel(QWidget* parent) : QWidget(parent) {
27   setupUi(this);
28 
29   recentProjectsGroup->setLayout(new QVBoxLayout);
30   newProjectLabel->setText(Utils::richTextForLink(newProjectLabel->text()));
31   openProjectLabel->setText(Utils::richTextForLink(openProjectLabel->text()));
32 
33   RecentProjects rp;
34   rp.read();
35   if (!rp.validate()) {
36     // Some project files weren't found.
37     // Write the list without them.
38     rp.write();
39   }
40   if (rp.isEmpty()) {
41     recentProjectsGroup->setVisible(false);
42   } else {
43     rp.enumerate([this](const QString& file_path) { addRecentProject(file_path); });
44   }
45 
46   connect(newProjectLabel, SIGNAL(linkActivated(const QString&)), this, SIGNAL(newProject()));
47   connect(openProjectLabel, SIGNAL(linkActivated(const QString&)), this, SIGNAL(openProject()));
48 }
49 
addRecentProject(const QString & file_path)50 void NewOpenProjectPanel::addRecentProject(const QString& file_path) {
51   const QFileInfo file_info(file_path);
52   QString base_name(file_info.completeBaseName());
53   if (base_name.isEmpty()) {
54     base_name = QChar('_');
55   }
56   auto* label = new QLabel(recentProjectsGroup);
57   label->setWordWrap(true);
58   label->setTextFormat(Qt::RichText);
59   label->setText(Utils::richTextForLink(base_name, file_path));
60   label->setToolTip(file_path);
61 
62   int fontSize = recentProjectsGroup->font().pointSize();
63   QFont widgetFont = label->font();
64   widgetFont.setPointSize(fontSize);
65   label->setFont(widgetFont);
66 
67   recentProjectsGroup->layout()->addWidget(label);
68 
69   connect(label, SIGNAL(linkActivated(const QString&)), this, SIGNAL(openRecentProject(const QString&)));
70 }
71 
paintEvent(QPaintEvent *)72 void NewOpenProjectPanel::paintEvent(QPaintEvent*) {
73   // In fact Qt doesn't draw QWidget's background, unless
74   // autoFillBackground property is set, so we can safely
75   // draw our borders and shadows in the margins area.
76 
77   int left = 0, top = 0, right = 0, bottom = 0;
78   layout()->getContentsMargins(&left, &top, &right, &bottom);
79 
80   const QRect widget_rect(rect());
81   const QRect except_margins(widget_rect.adjusted(left, top, -right, -bottom));
82 
83   const int border = 1;  // Solid line border width.
84 
85   QPainter painter(this);
86 
87   const QBrush border_brush
88       = ColorSchemeManager::instance()->getColorParam(ColorScheme::OpenNewProjectBorder, palette().windowText());
89   painter.setPen(QPen(border_brush, border));
90 
91   painter.drawRect(except_margins);
92 }  // NewOpenProjectPanel::paintEvent
93