1 #include "dlg_tip_of_the_day.h"
2 
3 #include "settingscache.h"
4 #include "tip_of_the_day.h"
5 
6 #include <QCheckBox>
7 #include <QDate>
8 #include <QDebug>
9 #include <QDialogButtonBox>
10 #include <QGridLayout>
11 #include <QLabel>
12 #include <QPushButton>
13 
14 #define MIN_TIP_IMAGE_HEIGHT 200
15 #define MIN_TIP_IMAGE_WIDTH 200
16 #define MAX_TIP_IMAGE_HEIGHT 300
17 #define MAX_TIP_IMAGE_WIDTH 500
18 
DlgTipOfTheDay(QWidget * parent)19 DlgTipOfTheDay::DlgTipOfTheDay(QWidget *parent) : QDialog(parent)
20 {
21     successfulInit = false;
22     QString xmlPath = "theme:tips/tips_of_the_day.xml";
23     tipDatabase = new TipsOfTheDay(xmlPath, this);
24 
25     if (tipDatabase->rowCount() == 0) {
26         return;
27     }
28 
29     title = new QLabel();
30     title->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
31     tipTextContent = new QLabel();
32     tipTextContent->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
33     tipTextContent->setWordWrap(true);
34     tipTextContent->setTextInteractionFlags(Qt::TextBrowserInteraction);
35     tipTextContent->setOpenExternalLinks(true);
36     imageLabel = new QLabel();
37     imageLabel->setFixedHeight(MAX_TIP_IMAGE_HEIGHT + 50);
38     imageLabel->setFixedWidth(MAX_TIP_IMAGE_WIDTH + 50);
39     image = new QPixmap();
40     date = new QLabel();
41     date->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
42     tipNumber = new QLabel();
43     tipNumber->setAlignment(Qt::AlignCenter);
44 
45     QList<int> seenTips = SettingsCache::instance().getSeenTips();
46     newTipsAvailable = false;
47     currentTip = 0;
48     for (int i = 0; i < tipDatabase->rowCount(); i++) {
49         if (!seenTips.contains(i)) {
50             newTipsAvailable = true;
51             currentTip = i;
52             break;
53         }
54     }
55 
56     connect(this, SIGNAL(newTipRequested(int)), this, SLOT(updateTip(int)));
57     newTipRequested(currentTip);
58 
59     content = new QVBoxLayout;
60     content->addWidget(title);
61     content->addWidget(tipTextContent);
62     content->addWidget(imageLabel);
63     content->addWidget(date);
64 
65     buttonBox = new QDialogButtonBox(Qt::Horizontal);
66     nextButton = new QPushButton(tr("Next"));
67     previousButton = new QPushButton(tr("Previous"));
68     buttonBox->addButton(previousButton, QDialogButtonBox::ActionRole);
69     buttonBox->addButton(nextButton, QDialogButtonBox::ActionRole);
70     buttonBox->addButton(QDialogButtonBox::Ok);
71     buttonBox->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
72     connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
73     connect(nextButton, SIGNAL(clicked()), this, SLOT(nextClicked()));
74     connect(previousButton, SIGNAL(clicked()), this, SLOT(previousClicked()));
75 
76     showTipsOnStartupCheck = new QCheckBox("Show tips on startup");
77     showTipsOnStartupCheck->setChecked(SettingsCache::instance().getShowTipsOnStartup());
78     connect(showTipsOnStartupCheck, SIGNAL(clicked(bool)), &SettingsCache::instance(),
79             SLOT(setShowTipsOnStartup(bool)));
80     buttonBar = new QHBoxLayout();
81     buttonBar->addWidget(showTipsOnStartupCheck);
82     buttonBar->addWidget(tipNumber);
83     buttonBar->addWidget(buttonBox);
84 
85     mainLayout = new QVBoxLayout;
86     mainLayout->addLayout(content);
87     mainLayout->addLayout(buttonBar);
88     setLayout(mainLayout);
89 
90     setWindowTitle(tr("Tip of the Day"));
91     setMinimumWidth(500);
92     setMinimumHeight(300);
93     successfulInit = true;
94 }
95 
~DlgTipOfTheDay()96 DlgTipOfTheDay::~DlgTipOfTheDay()
97 {
98     tipDatabase->deleteLater();
99     title->deleteLater();
100     tipTextContent->deleteLater();
101     imageLabel->deleteLater();
102     tipNumber->deleteLater();
103     showTipsOnStartupCheck->deleteLater();
104     content->deleteLater();
105     mainLayout->deleteLater();
106     buttonBox->deleteLater();
107     nextButton->deleteLater();
108     previousButton->deleteLater();
109     buttonBar->deleteLater();
110     delete image;
111 }
112 
nextClicked()113 void DlgTipOfTheDay::nextClicked()
114 {
115     emit newTipRequested(currentTip + 1);
116 }
117 
previousClicked()118 void DlgTipOfTheDay::previousClicked()
119 {
120     emit newTipRequested(currentTip - 1);
121 }
122 
updateTip(int tipId)123 void DlgTipOfTheDay::updateTip(int tipId)
124 {
125     QString titleText, contentText, imagePath;
126 
127     if (tipId < 0) {
128         tipId = tipDatabase->rowCount() - 1;
129     } else if (tipId >= tipDatabase->rowCount()) {
130         tipId = tipId % tipDatabase->rowCount();
131     }
132 
133     // Store tip id as seen
134     QList<int> seenTips = SettingsCache::instance().getSeenTips();
135     if (!seenTips.contains(tipId)) {
136         seenTips.append(tipId);
137         SettingsCache::instance().setSeenTips(seenTips);
138     }
139 
140     TipOfTheDay tip = tipDatabase->getTip(tipId);
141     titleText = tip.getTitle();
142     contentText = tip.getContent();
143     imagePath = tip.getImagePath();
144 
145     title->setText("<h2>" + titleText + "</h2>");
146     tipTextContent->setText(contentText);
147 
148     if (!image->load(imagePath)) {
149         qDebug() << "Image failed to load from" << imagePath;
150         imageLabel->clear();
151     } else {
152         int h = std::min(std::max(imageLabel->height(), MIN_TIP_IMAGE_HEIGHT), MAX_TIP_IMAGE_HEIGHT);
153         int w = std::min(std::max(imageLabel->width(), MIN_TIP_IMAGE_WIDTH), MAX_TIP_IMAGE_WIDTH);
154         imageLabel->setPixmap(image->scaled(w, h, Qt::KeepAspectRatio, Qt::SmoothTransformation));
155     }
156 
157     date->setText("<i>Tip added on: " + tip.getDate().toString("yyyy.MM.dd") + "</i>");
158 
159     tipNumber->setText("Tip " + QString::number(tipId + 1) + " / " + QString::number(tipDatabase->rowCount()));
160 
161     currentTip = static_cast<unsigned int>(tipId);
162 }
163 
resizeEvent(QResizeEvent * event)164 void DlgTipOfTheDay::resizeEvent(QResizeEvent *event)
165 {
166     imageLabel->setPixmap(image->scaled(imageLabel->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation));
167 
168     QWidget::resizeEvent(event);
169 }
170