00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include <qcheckbox.h>
00026 #include <qfile.h>
00027 #include <qhbox.h>
00028 #include <qlabel.h>
00029 #include <qlayout.h>
00030 #include <qpushbutton.h>
00031 #include <qtextstream.h>
00032 #include <qimage.h>
00033
00034 #include <kaboutdata.h>
00035 #include <kapplication.h>
00036 #include <kconfig.h>
00037 #include <kdebug.h>
00038 #include <kglobal.h>
00039 #include <kglobalsettings.h>
00040 #include <kiconloader.h>
00041 #include <klocale.h>
00042 #include <kpushbutton.h>
00043 #include <kseparator.h>
00044 #include <kstandarddirs.h>
00045 #include <kstdguiitem.h>
00046 #include <ktextbrowser.h>
00047 #include <kiconeffect.h>
00048 #include <kglobalsettings.h>
00049
00050 #include "ktip.h"
00051
00052
00053 KTipDatabase::KTipDatabase(const QString &_tipFile)
00054 {
00055 QString tipFile = _tipFile;
00056 if (tipFile.isEmpty())
00057 tipFile = QString::fromLatin1(KGlobal::instance()->aboutData()->appName()) + "/tips";
00058
00059 loadTips(tipFile);
00060
00061 if (tips.count())
00062 current = kapp->random() % tips.count();
00063 }
00064
00065
00066
00067
00068
00069 void KTipDatabase::loadTips(const QString &tipFile)
00070 {
00071 QString fileName = locate("data", tipFile);
00072
00073 if (fileName.isEmpty())
00074 {
00075 kdDebug() << "can't find '" << tipFile << "' in standard dirs" << endl;
00076 return;
00077 }
00078
00079 QFile file(fileName);
00080 if (!file.open(IO_ReadOnly))
00081 {
00082 kdDebug() << "can't open '" << fileName << "' for reading" << endl;
00083 return;
00084 }
00085
00086 tips.clear();
00087
00088 QString content = file.readAll();
00089
00090 int pos = -1;
00091 while ((pos = content.find("<html>", pos + 1, false)) != -1)
00092 {
00093 QString tip = content.mid(pos + 6, content.find("</html>", pos, false) - pos - 6);
00094 if (tip.startsWith("\n"))
00095 tip = tip.mid(1);
00096 tips.append(tip);
00097 }
00098
00099 file.close();
00100 }
00101
00102 void KTipDatabase::nextTip()
00103 {
00104 if (tips.count() == 0)
00105 return ;
00106 current += 1;
00107 if (current >= (int) tips.count())
00108 current = 0;
00109 }
00110
00111
00112 void KTipDatabase::prevTip()
00113 {
00114 if (tips.count() == 0)
00115 return ;
00116 current -= 1;
00117 if (current < 0)
00118 current = tips.count() - 1;
00119 }
00120
00121
00122 QString KTipDatabase::tip() const
00123 {
00124 return tips[current];
00125 }
00126
00127 KTipDialog *KTipDialog::_instance = 0;
00128
00129
00130 KTipDialog::KTipDialog(KTipDatabase *db, QWidget *parent, const char *name)
00131 : KDialog(parent, name)
00132 {
00137 bool isTipDialog = (parent != 0);
00138
00139 QImage img;
00140 int h,s,v;
00141
00142 _blendedColor = KGlobalSettings::activeTitleColor();
00143 _blendedColor.hsv(&h,&s,&v);
00144 _blendedColor.setHsv(h,s*(71/76.0),v*(67/93.0));
00145
00146 if (!isTipDialog)
00147 {
00148 img = QImage(locate("data", "kdewizard/pics/wizard_small.png"));
00149
00150 KIconEffect::colorize(img, _blendedColor, 1.0);
00151 QRgb colPixel( img.pixel(0,0) );
00152
00153 _blendedColor = QColor(qRed(colPixel),qGreen(colPixel),qBlue(colPixel));
00154 }
00155
00156 _baseColor = KGlobalSettings::alternateBackgroundColor();
00157 _baseColor.hsv(&h,&s,&v);
00158 _baseColor.setHsv(h,s*(10/6.0),v*(93/99.0));
00159
00160 _textColor = KGlobalSettings::textColor();
00161
00162
00163 _database = db;
00164
00165 setCaption(i18n("Tip of the Day"));
00166 setIcon(KGlobal::iconLoader()->loadIcon("ktip", KIcon::Small));
00167
00168 QVBoxLayout *vbox = new QVBoxLayout(this, marginHint(), spacingHint());
00169
00170 if (isTipDialog)
00171 {
00172 QHBoxLayout *pl = new QHBoxLayout(vbox, 0, 0);
00173
00174 QLabel *bulb = new QLabel(this);
00175 bulb->setPixmap(locate("data", "kdeui/pics/ktip-bulb.png"));
00176 pl->addWidget(bulb);
00177
00178 QLabel *titlePane = new QLabel(this);
00179 titlePane->setBackgroundPixmap(locate("data", "kdeui/pics/ktip-background.png"));
00180 titlePane->setText(i18n("Did you know...?\n"));
00181 titlePane->setFont(QFont(KGlobalSettings::generalFont().family(), 20, QFont::Bold));
00182 titlePane->setAlignment(QLabel::AlignCenter);
00183 pl->addWidget(titlePane, 100);
00184 }
00185
00186 QHBox *hbox = new QHBox(this);
00187 hbox->setSpacing(0);
00188 hbox->setFrameStyle(QFrame::Panel | QFrame::Sunken);
00189 vbox->addWidget(hbox);
00190
00191 QHBox *tl = new QHBox(hbox);
00192 tl->setMargin(7);
00193 tl->setBackgroundColor(_blendedColor);
00194
00195 QHBox *topLeft = new QHBox(tl);
00196 topLeft->setMargin(15);
00197 topLeft->setBackgroundColor(_baseColor);
00198
00199 _tipText = new KTextBrowser(topLeft);
00200
00201 _tipText->setWrapPolicy( QTextEdit::AtWordOrDocumentBoundary );
00202 _tipText->mimeSourceFactory()->addFilePath(
00203 KGlobal::dirs()->findResourceDir("data", "kdewizard/pics")+"kdewizard/pics/");
00204 _tipText->setFrameStyle(QFrame::NoFrame | QFrame::Plain);
00205 _tipText->setHScrollBarMode(QScrollView::AlwaysOff);
00206 _tipText->setLinkUnderline(false);
00207
00208 QStyleSheet *sheet = _tipText->styleSheet();
00209 QStyleSheetItem *item = sheet->item("a");
00210 item->setFontWeight(QFont::Bold);
00211 _tipText->setStyleSheet(sheet);
00212 QPalette pal = _tipText->palette();
00213 pal.setColor( QPalette::Active, QColorGroup::Link, _blendedColor );
00214 pal.setColor( QPalette::Inactive, QColorGroup::Link, _blendedColor );
00215 _tipText->setPalette(pal);
00216
00217 QStringList icons = KGlobal::dirs()->resourceDirs("icon");
00218 QStringList::Iterator it;
00219 for (it = icons.begin(); it != icons.end(); ++it)
00220 _tipText->mimeSourceFactory()->addFilePath(*it);
00221
00222 if (!isTipDialog)
00223 {
00224 QLabel *l = new QLabel(hbox);
00225 l->setPixmap(img);
00226 l->setBackgroundColor(_blendedColor);
00227 l->setAlignment(Qt::AlignRight | Qt::AlignBottom);
00228
00229 resize(550, 230);
00230 QSize sh = size();
00231 QRect rect = KApplication::desktop()->screenGeometry();
00232 move(rect.x() + (rect.width() - sh.width())/2,
00233 rect.y() + (rect.height() - sh.height())/2);
00234 }
00235
00236 KSeparator* sep = new KSeparator( KSeparator::HLine, this);
00237 vbox->addWidget(sep);
00238
00239 QHBoxLayout *hbox2 = new QHBoxLayout(vbox, 4);
00240
00241 _tipOnStart = new QCheckBox(i18n("&Show tips on startup"), this);
00242 hbox2->addWidget(_tipOnStart, 1);
00243
00244 KPushButton *prev = new KPushButton( KStdGuiItem::back(
00245 KStdGuiItem::UseRTL ), this );
00246 prev->setText( i18n("&Previous") );
00247 hbox2->addWidget(prev);
00248
00249 KPushButton *next = new KPushButton( KStdGuiItem::forward(
00250 KStdGuiItem::UseRTL ), this );
00251 next->setText( i18n("&Next") );
00252 hbox2->addWidget(next);
00253
00254 KPushButton *ok = new KPushButton(KStdGuiItem::close(), this);
00255 ok->setDefault(true);
00256 hbox2->addWidget(ok);
00257
00258 KConfigGroup config(kapp->config(), "TipOfDay");
00259 _tipOnStart->setChecked(config.readBoolEntry("RunOnStart", true));
00260
00261 connect(next, SIGNAL(clicked()), this, SLOT(nextTip()));
00262 connect(prev, SIGNAL(clicked()), this, SLOT(prevTip()));
00263 connect(ok, SIGNAL(clicked()), this, SLOT(accept()));
00264 connect(_tipOnStart, SIGNAL(toggled(bool)), this, SLOT(showOnStart(bool)));
00265
00266 ok->setFocus();
00267
00268 nextTip();
00269 }
00270
00271
00272 void KTipDialog::showTip(const QString &tipFile, bool force)
00273 {
00274 showTip(kapp->mainWidget(), tipFile, force);
00275 }
00276
00277 void KTipDialog::showTip(QWidget *parent,const QString &tipFile, bool force)
00278 {
00279 if (!force)
00280 {
00281 KConfigGroup config(kapp->config(), "TipOfDay");
00282 if (!config.readBoolEntry("RunOnStart", true))
00283 return;
00284 }
00285
00286 if (!_instance)
00287 _instance = new KTipDialog(new KTipDatabase(tipFile), parent);
00288
00289 _instance->nextTip();
00290 _instance->show();
00291 _instance->raise();
00292 }
00293
00294 void KTipDialog::prevTip()
00295 {
00296 _database->prevTip();
00297 _tipText->setText(QString::fromLatin1(
00298 "<qt text=\"%1\" bgcolor=\"%2\">%3</qt>")
00299 .arg(_textColor.name())
00300 .arg(_baseColor.name())
00301 .arg(i18n(_database->tip().utf8())));
00302 }
00303
00304 void KTipDialog::nextTip()
00305 {
00306 _database->nextTip();
00307 _tipText->setText(QString::fromLatin1("<qt text=\"%1\" bgcolor=\"%2\">%3</qt>")
00308 .arg(_textColor.name())
00309 .arg(_baseColor.name())
00310 .arg(i18n(_database->tip().utf8())));
00311 }
00312
00313 void KTipDialog::showOnStart(bool on)
00314 {
00315 setShowOnStart(on);
00316 }
00317
00318 void KTipDialog::setShowOnStart(bool on)
00319 {
00320 KConfigGroup config(kapp->config(), "TipOfDay");
00321 config.writeEntry("RunOnStart", on);
00322 config.sync();
00323 }
00324
00325 bool KTipDialog::eventFilter(QObject *o, QEvent *e)
00326 {
00327 if (o == _tipText && e->type()== QEvent::KeyPress &&
00328 (((QKeyEvent *)e)->key() == Key_Return ||
00329 ((QKeyEvent *)e)->key() == Key_Space ))
00330 accept();
00331
00332
00333
00334
00335
00336
00337
00338 return QWidget::eventFilter( o, e );
00339 }
00340
00341 void KTipDialog::virtual_hook( int id, void* data )
00342 {
00343 KDialog::virtual_hook( id, data );
00344 }
00345
00346 #include "ktip.moc"