ktip.cpp Source File (kdeui Library)

| # kdeui Library API Documentation
Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Source Files

# ktip.cpp

    00001 /*****************************************************************
    00002
    00003 Copyright (c) 2000, 2001 Matthias Hoelzer-Kluepfel
    00004                          Tobias Koenig <tokoe82@yahoo.de>
    00005
    00006 Permission is hereby granted, free of charge, to any person obtaining a copy
    00007 of this software and associated documentation files (the "Software"), to deal
    00008 in the Software without restriction, including without limitation the rights
    00009 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    00010 copies of the Software, and to permit persons to whom the Software is
    00011 furnished to do so, subject to the following conditions:
    00012
    00013 The above copyright notice and this permission notice shall be included in
    00014 all copies or substantial portions of the Software.
    00015
    00016 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    00017 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    00018 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
    00019 AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
    00020 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
    00021 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    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 // if you change something here, please update the script
    00067 // preparetips, which depends on extracting exactly the same
    00068 // text as done here.
    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         // colorize and check to figure the correct color
    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 KTipDialog::~KTipDialog()
    00273 {
    00274     if( _instance==this )
    00275         _instance = 0L;
    00276 }
    00277
    00278 void KTipDialog::showTip(const QString &tipFile, bool force)
    00279 {
    00280     showTip(kapp->mainWidget(), tipFile, force);
    00281 }
    00282
    00283 void KTipDialog::showTip(QWidget *parent,const QString &tipFile, bool force)
    00284 {
    00285     if (!force)
    00286     {
    00287         KConfigGroup config(kapp->config(), "TipOfDay");
    00288         if (!config.readBoolEntry("RunOnStart", true))
    00289             return;
    00290     }
    00291
    00292     if (!_instance)
    00293         _instance = new KTipDialog(new KTipDatabase(tipFile), parent);
    00294
    00295     _instance->nextTip();
    00296     _instance->show();
    00297     _instance->raise();
    00298 }
    00299
    00300 void KTipDialog::prevTip()
    00301 {
    00302     _database->prevTip();
    00303     _tipText->setText(QString::fromLatin1(
    00304          "<qt text=\"%1\" bgcolor=\"%2\">%3</qt>")
    00305          .arg(_textColor.name())
    00306          .arg(_baseColor.name())
    00307          .arg(i18n(_database->tip().utf8())));
    00308 }
    00309
    00310 void KTipDialog::nextTip()
    00311 {
    00312     _database->nextTip();
    00313     _tipText->setText(QString::fromLatin1("<qt text=\"%1\" bgcolor=\"%2\">%3</qt>")
    00314                         .arg(_textColor.name())
    00315                         .arg(_baseColor.name())
    00316                         .arg(i18n(_database->tip().utf8())));
    00317 }
    00318
    00319 void KTipDialog::showOnStart(bool on)
    00320 {
    00321     setShowOnStart(on);
    00322 }
    00323
    00324 void KTipDialog::setShowOnStart(bool on)
    00325 {
    00326     KConfigGroup config(kapp->config(), "TipOfDay");
    00327     config.writeEntry("RunOnStart", on);
    00328     config.sync();
    00329 }
    00330
    00331 bool KTipDialog::eventFilter(QObject *o, QEvent *e)
    00332 {
    00333         if (o == _tipText && e->type()== QEvent::KeyPress &&
    00334                 (((QKeyEvent *)e)->key() == Key_Return ||
    00335                 ((QKeyEvent *)e)->key() == Key_Space ))
    00336                 accept();
    00337
    00338         // If the user presses Return or Space, we close the dialog as if the
    00339         // default button was pressed even if the KTextBrowser has the keyboard
    00340         // focus. This could have the bad side-effect that the user cannot use the
    00341         // keyboard to open urls in the KTextBrowser, so we just let it handle
    00342         // the key event _additionally_. (Antonio)
    00343
    00344         return QWidget::eventFilter( o, e );
    00345 }
    00346
    00347 void KTipDialog::virtual_hook( int id, void* data )
    00348 {
    00349         KDialog::virtual_hook( id, data );
    00350 }
    00351
    00352 #include "ktip.moc"

This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Jan 28 12:57:49 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001