1
//MountManager - the program for easy mounting of storage devices in Linux
2
//Copyright (C) 2007-2008 Tikhonov Sergey
4
//This file is part of MountManager Plugin "Tips of the day"
6
//This program is free software; you can redistribute it and/or
7
//modify it under the terms of the GNU General Public License
8
//as published by the Free Software Foundation; either version 2
9
//of the License, or (at your option) any later version.
11
//This program is distributed in the hope that it will be useful,
12
//but WITHOUT ANY WARRANTY; without even the implied warranty of
13
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
//GNU General Public License for more details.
16
//You should have received a copy of the GNU General Public License
17
//along with this program; if not, write to the Free Software
18
//Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
#include <QtGui/QDialog>
20
#include <QtGui/QAction>
21
#include <QtGui/QLabel>
22
#include <QtGui/QTextBrowser>
23
#include <QtGui/QCheckBox>
24
#include <QtGui/QPushButton>
25
#include <QtGui/QVBoxLayout>
26
#include <QtGui/QHBoxLayout>
27
#include <QtCore/QDir>
28
#include <QtCore/QFile>
29
#include <QtCore/QTextStream>
30
#include <QtCore/QSettings>
33
#include "../../sources/gui/const.h"
36
TipsDialog::TipsDialog(QWidget *parent) {
37
QFile file(":/resources/tips.txt");
38
file.open(QIODevice::ReadOnly);
39
QTextStream stream(&file);
42
int emptyLinesInRow = 0;
43
QString currentTipText;
44
while (!stream.atEnd()) {
45
line = stream.readLine();
50
currentTipText += line;
51
if (emptyLinesInRow == 2 || (!currentTipText.isEmpty() && stream.atEnd()))
52
if (!currentTipText.isEmpty()) {
53
tips.append(new Tip(currentTipText,tips.count()));
54
currentTipText.clear();
58
mainWidget = new QDialog(parent);
60
textBrowser = new QTextBrowser;
62
showOnStartup = new QCheckBox(tr("Show tips on startup"));
64
nextButton = new QPushButton;
65
nextButton->setIcon(QIcon(":/resources/next.png"));
66
nextButton->setFlat(true);
67
nextButton->setEnabled(tips.count() > 0);
68
connect(nextButton,SIGNAL(clicked()),this,SLOT(next()));
70
backButton = new QPushButton;
71
backButton->setIcon(QIcon(":/resources/previous.png"));
72
backButton->setFlat(true);
73
backButton->setEnabled(false);
74
connect(backButton,SIGNAL(clicked()),textBrowser,SLOT(backward()));
75
connect(textBrowser,SIGNAL(backwardAvailable(bool)),backButton,SLOT(setEnabled(bool)));
77
closeButton = new QPushButton;
78
closeButton->setIcon(QIcon(":/resources/close.png"));
79
closeButton->setFlat(true);
80
connect(closeButton,SIGNAL(clicked()),mainWidget,SLOT(hide()));
82
QLabel *headerLabel = new QLabel("<font size='4'><center><b>" + tr("Did you know?") + "</b></center></font>");
84
QAction *action = new QAction(this);
85
action->setText(tr("Tips of the day"));
86
action->setIcon(QIcon(":/resources/main.png"));
87
connect(action,SIGNAL(triggered()),this,SLOT(show()));
88
actions.append(action);
90
QHBoxLayout *buttonLayout = new QHBoxLayout;
91
buttonLayout->addWidget(showOnStartup);
92
buttonLayout->addStretch();
93
buttonLayout->addWidget(backButton);
94
buttonLayout->addWidget(nextButton);
95
buttonLayout->addWidget(closeButton);
96
buttonLayout->setMargin(0);
98
QVBoxLayout *mainLayout = new QVBoxLayout;
99
mainLayout->addWidget(headerLabel);
100
mainLayout->addWidget(textBrowser);
101
mainLayout->addLayout(buttonLayout);
103
mainWidget->setLayout(mainLayout);
105
mainWidget->setWindowTitle(tr("Tips of the day"));
106
mainWidget->setWindowIcon(QIcon(":/resources/main.png"));
110
TipsDialog::~TipsDialog() {
111
foreach (QAction *action,actions)
114
delete showOnStartup;
121
void TipsDialog::setParent(QObject *parent) {
122
QSettings settings(ORGANIZATION,PROGRAM_NAME);
123
if (settings.value("Tips/ShowOnStartup",true).toBool())
124
connect(parent,SIGNAL(started()),this,SLOT(show()));
125
connect(parent,SIGNAL(finished()),this,SLOT(saveSettings()));
128
const QString TipsDialog::pluginName() const {
129
return tr("Tips of the day");
132
const QString TipsDialog::pluginDescription() const {
133
return tr("You can read different advices for usage of MountManager and information about mounting in Linux.");
136
QDialog* TipsDialog::dialog() const {
140
QDockWidget* TipsDialog::dockWidget() const {
144
QMenu* TipsDialog::menu() const {
148
QToolBar* TipsDialog::toolBar() const {
152
Actions TipsDialog::menuActions() const {
156
int TipsDialog::randomIndex() {
157
if (watchedTipsIndexes.count() == tips.count())
158
watchedTipsIndexes.clear();
159
int randomNumber = (int)(double(tips.count())*rand()/(RAND_MAX+1.0));
160
while (watchedTipsIndexes.contains(randomNumber))
161
randomNumber = (int)(double(tips.count())*rand()/(RAND_MAX+1.0));
165
void TipsDialog::show() {
170
void TipsDialog::next() {
171
int index = randomIndex();
172
watchedTipsIndexes.append(index);
173
textBrowser->setHtml(tips[index]->tipContent);
176
void TipsDialog::saveSettings() {
177
QSettings settings(ORGANIZATION,PROGRAM_NAME);
178
settings.setValue("Tips/ShowOnStartup",showOnStartup->isChecked());
179
settings.setValue("Tips/Size",mainWidget->size());
182
void TipsDialog::loadSettings() {
183
QSettings settings(ORGANIZATION,PROGRAM_NAME);
184
showOnStartup->setChecked(settings.value("Tips/ShowOnStartup",true).toBool());
185
mainWidget->resize(settings.value("Tips/Size",QSize(450,300)).toSize());
188
Q_EXPORT_PLUGIN2(tipsplugin,TipsDialog)