• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

KDEUI

  • kdeui
  • dialogs
ktip.cpp
Go to the documentation of this file.
1/*****************************************************************
2
3Copyright (c) 2000-2003 Matthias Hoelzer-Kluepfel <mhk@kde.org>
4 Tobias Koenig <tokoe@kde.org>
5 Daniel Molkentin <molkentin@kde.org>
6Copyright (c) 2008 Urs Wolfer <uwolfer @ kde.org>
7
8Permission is hereby granted, free of charge, to any person obtaining a copy
9of this software and associated documentation files (the "Software"), to deal
10in the Software without restriction, including without limitation the rights
11to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12copies of the Software, and to permit persons to whom the Software is
13furnished to do so, subject to the following conditions:
14
15The above copyright notice and this permission notice shall be included in
16all copies or substantial portions of the Software.
17
18THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
25******************************************************************/
26
27#include "ktip.h"
28
29#include <QtCore/QFile>
30#include <QtGui/QCheckBox>
31#include <QtGui/QKeyEvent>
32#include <QtGui/QLabel>
33#include <QtGui/QLayout>
34
35#include <kaboutdata.h>
36#include <kconfig.h>
37#include <kdebug.h>
38#include <kglobalsettings.h>
39#include <kcomponentdata.h>
40#include <klocale.h>
41#include <kpushbutton.h>
42#include <krandom.h>
43#include <kseparator.h>
44#include <kstandarddirs.h>
45#include <ktextbrowser.h>
46
47class KTipDatabase::Private
48{
49 public:
50 void loadTips( const QString &tipFile );
51 void addTips( const QString &tipFile );
52
53 QStringList tips;
54 int currentTip;
55};
56
57void KTipDatabase::Private::loadTips( const QString &tipFile )
58{
59 tips.clear();
60 addTips( tipFile );
61}
62
68void KTipDatabase::Private::addTips( const QString &tipFile )
69{
70 QString fileName = KStandardDirs::locate( "data", tipFile );
71
72 if ( fileName.isEmpty() ) {
73 kDebug() << "KTipDatabase::addTips: can't find '" << tipFile << "' in standard dirs";
74 return;
75 }
76
77 QFile file( fileName );
78 if ( !file.open( QIODevice::ReadOnly ) ) {
79 kDebug() << "KTipDatabase::addTips: can't open '" << fileName << "' for reading";
80 return;
81 }
82
83 QByteArray data = file.readAll();
84 QString content = QString::fromUtf8( data.constData(), data.size() );
85 const QRegExp rx( "\\n+" );
86
87 int pos = -1;
88 while ( ( pos = content.indexOf( "<html>", pos + 1, Qt::CaseInsensitive ) ) != -1 ) {
93 QString tip = content
94 .mid( pos + 6, content.indexOf( "</html>", pos, Qt::CaseInsensitive ) - pos - 6 )
95 .replace( rx, "\n" );
96
97 if ( !tip.endsWith( '\n' ) )
98 tip += '\n';
99
100 if ( tip.startsWith( '\n' ) )
101 tip = tip.mid( 1 );
102
103 if ( tip.isEmpty() ) {
104 kDebug() << "Empty tip found! Skipping! " << pos;
105 continue;
106 }
107
108 tips.append( tip );
109 }
110
111 file.close();
112}
113
114
115KTipDatabase::KTipDatabase( const QString &_tipFile )
116 : d( new Private )
117{
118 QString tipFile = _tipFile;
119
120 if ( tipFile.isEmpty() )
121 tipFile = KGlobal::mainComponent().aboutData()->appName() + "/tips";
122
123 d->loadTips( tipFile );
124
125 if ( !d->tips.isEmpty() )
126 d->currentTip = KRandom::random() % d->tips.count();
127}
128
129KTipDatabase::KTipDatabase( const QStringList& tipsFiles )
130 : d( new Private )
131{
132 if ( tipsFiles.isEmpty() || ( ( tipsFiles.count() == 1 ) && tipsFiles.first().isEmpty() ) ) {
133 d->addTips( KGlobal::mainComponent().aboutData()->appName() + "/tips" );
134 } else {
135 for ( QStringList::ConstIterator it = tipsFiles.begin(); it != tipsFiles.end(); ++it )
136 d->addTips( *it );
137 }
138
139 if ( !d->tips.isEmpty() )
140 d->currentTip = KRandom::random() % d->tips.count();
141}
142
143KTipDatabase::~KTipDatabase()
144{
145 delete d;
146}
147
148void KTipDatabase::nextTip()
149{
150 if ( d->tips.isEmpty() )
151 return;
152
153 d->currentTip += 1;
154
155 if ( d->currentTip >= (int) d->tips.count() )
156 d->currentTip = 0;
157}
158
159void KTipDatabase::prevTip()
160{
161 if ( d->tips.isEmpty() )
162 return;
163
164 d->currentTip -= 1;
165
166 if ( d->currentTip < 0 )
167 d->currentTip = d->tips.count() - 1;
168}
169
170QString KTipDatabase::tip() const
171{
172 if ( d->tips.isEmpty() )
173 return QString();
174
175 return d->tips[ d->currentTip ];
176}
177
178
179class KTipDialog::Private
180{
181 public:
182 Private( KTipDialog *_parent )
183 : parent( _parent )
184 {
185 }
186 ~Private()
187 {
188 delete database;
189 }
190
191 void _k_nextTip();
192 void _k_prevTip();
193 void _k_showOnStart( bool );
194
195 KTipDialog *parent;
196 KTipDatabase *database;
197 QCheckBox *tipOnStart;
198 KTextBrowser *tipText;
199
200 static KTipDialog *mInstance;
201};
202
203KTipDialog *KTipDialog::Private::mInstance = 0;
204
205void KTipDialog::Private::_k_prevTip()
206{
207 database->prevTip();
208 tipText->setHtml( QString::fromLatin1( "<html><body>%1</body></html>" )
209 .arg( i18n( database->tip().toUtf8() ) ) );
210}
211
212void KTipDialog::Private::_k_nextTip()
213{
214 database->nextTip();
215 tipText->setHtml( QString::fromLatin1( "<html><body>%1</body></html>" )
216 .arg( i18n( database->tip().toUtf8() ) ) );
217}
218
219void KTipDialog::Private::_k_showOnStart( bool on )
220{
221 parent->setShowOnStart( on );
222}
223
224
225KTipDialog::KTipDialog( KTipDatabase *database, QWidget *parent )
226 : KDialog( parent ),
227 d( new Private( this ) )
228{
229 setButtons( KDialog::None );
230 setCaption( i18n( "Tip of the Day" ) );
231
236 bool isTipDialog = (parent != 0);
237
238 d->database = database;
239
240 setWindowIcon(KIcon("ktip"));
241
242 QWidget *widget = new QWidget( this );
243 setMainWidget( widget );
244 QVBoxLayout *mainLayout = new QVBoxLayout( widget );
245 mainLayout->setMargin( 0 );
246
247 if ( isTipDialog ) {
248 QLabel *titleLabel = new QLabel( this );
249 titleLabel->setText( i18n( "Did you know...?\n" ) );
250 titleLabel->setFont( QFont( KGlobalSettings::generalFont().family(), 20, QFont::Bold ) );
251 titleLabel->setAlignment( Qt::AlignCenter );
252 mainLayout->addWidget( titleLabel );
253 }
254
255 QHBoxLayout *browserLayout = new QHBoxLayout();
256 mainLayout->addLayout( browserLayout );
257
258 d->tipText = new KTextBrowser( this );
259
260 d->tipText->setOpenExternalLinks( true );
261
262 d->tipText->setWordWrapMode( QTextOption::WrapAtWordBoundaryOrAnywhere );
263
264 QStringList paths;
265 paths << KGlobal::dirs()->resourceDirs( "icon" )
266 << KGlobal::dirs()->findResourceDir( "data", "kdewizard/pics" ) + "kdewizard/pics/";
267
268 d->tipText->setSearchPaths( paths );
269
270 d->tipText->setFrameStyle( QFrame::NoFrame );
271 d->tipText->setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
272 QPalette tipPal(d->tipText->palette());
273 tipPal.setColor(QPalette::Base, Qt::transparent);
274 tipPal.setColor(QPalette::Text, tipPal.color(QPalette::WindowText));
275 d->tipText->setPalette(tipPal);
276
277 browserLayout->addWidget( d->tipText );
278
279 QLabel *label = new QLabel( this );
280 label->setPixmap( KStandardDirs::locate( "data", "kdeui/pics/ktip-bulb.png" ) );
281 label->setAlignment( Qt::AlignRight | Qt::AlignVCenter );
282 browserLayout->addWidget( label );
283
284 if ( !isTipDialog ) {
285 resize( 520, 280 );
286 QSize sh = size();
287
288 QRect rect = KGlobalSettings::splashScreenDesktopGeometry();
289
290 move( rect.x() + ( rect.width() - sh.width() ) / 2,
291 rect.y() + ( rect.height() - sh.height() ) / 2 );
292 }
293
294 KSeparator* sep = new KSeparator( Qt::Horizontal );
295 mainLayout->addWidget( sep );
296
297 QHBoxLayout *buttonLayout = new QHBoxLayout();
298
299 mainLayout->addLayout( buttonLayout );
300
301 d->tipOnStart = new QCheckBox( i18n( "&Show tips on startup" ) );
302 buttonLayout->addWidget( d->tipOnStart, 1 );
303
304 KPushButton *prev = new KPushButton( KStandardGuiItem::back( KStandardGuiItem::UseRTL ) );
305 prev->setText( i18n( "&Previous" ) );
306 buttonLayout->addWidget( prev );
307
308 KPushButton *next = new KPushButton( KStandardGuiItem::forward( KStandardGuiItem::UseRTL ));
309 next->setText( i18nc( "Opposite to Previous", "&Next" ) );
310 buttonLayout->addWidget( next );
311
312 KPushButton *ok = new KPushButton( KStandardGuiItem::close());
313 ok->setDefault( true );
314 buttonLayout->addWidget( ok );
315
316 KConfigGroup config( KGlobal::config(), "TipOfDay" );
317 d->tipOnStart->setChecked( config.readEntry( "RunOnStart", true ) );
318
319 connect( next, SIGNAL(clicked()), this, SLOT(_k_nextTip()) );
320 connect( prev, SIGNAL(clicked()), this, SLOT(_k_prevTip()) );
321 connect( ok, SIGNAL(clicked()), this, SLOT(accept()) );
322 connect( d->tipOnStart, SIGNAL(toggled(bool)), this, SLOT(_k_showOnStart(bool)) );
323
324 ok->setFocus();
325
326 d->_k_nextTip();
327}
328
329KTipDialog::~KTipDialog()
330{
331 if ( Private::mInstance == this )
332 Private::mInstance = 0L;
333 delete d;
334}
335
340void KTipDialog::showTip( const QString &tipFile, bool force )
341{
342 showTip( 0, tipFile, force );
343}
344
345void KTipDialog::showTip( QWidget *parent, const QString &tipFile, bool force )
346{
347 showMultiTip( parent, QStringList( tipFile ), force );
348}
349
350void KTipDialog::showMultiTip( QWidget *parent, const QStringList &tipFiles, bool force )
351{
352 KConfigGroup configGroup( KGlobal::config(), "TipOfDay" );
353
354 const bool runOnStart = configGroup.readEntry( "RunOnStart", true );
355
356 if ( !force ) {
357 if ( !runOnStart )
358 return;
359
360 // showing the tooltips on startup suggests the tooltip
361 // will be shown *each time* on startup, not $random days later
362 // TODO either remove or uncomment this code, but make the situation clear
363 /*bool hasLastShown = configGroup.hasKey( "TipLastShown" );
364 if ( hasLastShown ) {
365 const int oneDay = 24 * 60 * 60;
366 QDateTime lastShown = configGroup.readEntry( "TipLastShown", QDateTime() );
367
368 // Show tip roughly once a week
369 if ( lastShown.secsTo( QDateTime::currentDateTime() ) < (oneDay + (KRandom::random() % (10 * oneDay))) )
370 return;
371 }
372
373 configGroup.writeEntry( "TipLastShown", QDateTime::currentDateTime() );
374
375 if ( !hasLastShown )
376 return; // Don't show tip on first start*/
377 }
378
379 if ( !Private::mInstance )
380 Private::mInstance = new KTipDialog( new KTipDatabase( tipFiles ), parent );
381 else
382 // The application might have changed the RunOnStart option in its own
383 // configuration dialog, so we should update the checkbox.
384 Private::mInstance->d->tipOnStart->setChecked( runOnStart );
385
386 Private::mInstance->show();
387 Private::mInstance->raise();
388}
389
390void KTipDialog::setShowOnStart( bool on )
391{
392 KConfigGroup config( KGlobal::config(), "TipOfDay" );
393 config.writeEntry( "RunOnStart", on );
394}
395
396bool KTipDialog::eventFilter( QObject *object, QEvent *event )
397{
398 if ( object == d->tipText && event->type() == QEvent::KeyPress &&
399 (((QKeyEvent *)event)->key() == Qt::Key_Return ||
400 ((QKeyEvent *)event)->key() == Qt::Key_Space ))
401 accept();
402
411 return QWidget::eventFilter( object, event );
412}
413
414
415#include "ktip.moc"
KAboutData::appName
QString appName() const
KComponentData::aboutData
const KAboutData * aboutData() const
KConfigGroup
KConfigGroup::readEntry
QString readEntry(const char *key, const char *aDefault=0) const
KDialog
A dialog base class with standard buttons and predefined layouts.
Definition: kdialog.h:129
KDialog::setMainWidget
void setMainWidget(QWidget *widget)
Sets the main widget of the dialog.
Definition: kdialog.cpp:338
KDialog::setButtons
void setButtons(ButtonCodes buttonMask)
Creates (or recreates) the button box and all the buttons in it.
Definition: kdialog.cpp:206
KDialog::None
@ None
Definition: kdialog.h:138
KDialog::setCaption
virtual void setCaption(const QString &caption)
Make a KDE compliant caption.
Definition: kdialog.cpp:469
KGlobalSettings::generalFont
static QFont generalFont()
Returns the default general font.
Definition: kglobalsettings.cpp:446
KGlobalSettings::splashScreenDesktopGeometry
static QRect splashScreenDesktopGeometry()
This function returns the desktop geometry for an application's splash screen.
Definition: kglobalsettings.cpp:713
KIcon
A wrapper around QIcon that provides KDE icon features.
Definition: kicon.h:41
KPushButton
A QPushButton with drag-support and KGuiItem support.
Definition: kpushbutton.h:47
KPushButton::setText
void setText(const QString &text)
Sets the text of the button.
Definition: kpushbutton.cpp:239
KSeparator
Standard horizontal or vertical separator.
Definition: kseparator.h:35
KStandardDirs::findResourceDir
QString findResourceDir(const char *type, const QString &filename) const
KStandardDirs::locate
static QString locate(const char *type, const QString &filename, const KComponentData &cData=KGlobal::mainComponent())
KStandardDirs::resourceDirs
QStringList resourceDirs(const char *type) const
KTextBrowser
Extended QTextBrowser.
Definition: ktextbrowser.h:52
KTipDatabase
A database for tips-of-the-day.
Definition: ktip.h:54
KTipDatabase::prevTip
void prevTip()
The previous tip will become the current one.
Definition: ktip.cpp:159
KTipDatabase::~KTipDatabase
~KTipDatabase()
Definition: ktip.cpp:143
KTipDatabase::tip
QString tip() const
Returns the current tip.
Definition: ktip.cpp:170
KTipDatabase::KTipDatabase
KTipDatabase(const QString &tipFile=QString())
This constructor reads in the tips from a file with the given name.
Definition: ktip.cpp:115
KTipDatabase::nextTip
void nextTip()
The next tip will become the current one.
Definition: ktip.cpp:148
KTipDialog
A Tip-of-the-Day dialog.
Definition: ktip.h:104
KTipDialog::KTipDialog
KTipDialog(KTipDatabase *database, QWidget *parent=0)
Construct a tip dialog.
Definition: ktip.cpp:225
KTipDialog::setShowOnStart
static void setShowOnStart(bool show)
Toggles the start behavior.
Definition: ktip.cpp:390
KTipDialog::~KTipDialog
~KTipDialog()
Destroys the tip dialog.
Definition: ktip.cpp:329
KTipDialog::showMultiTip
static void showMultiTip(QWidget *parent, const QStringList &tipFiles, bool force=false)
Shows a tip.
Definition: ktip.cpp:350
KTipDialog::showTip
static void showTip(QWidget *parent, const QString &tipFile=QString(), bool force=false)
Shows a tip.
Definition: ktip.cpp:345
KTipDialog::eventFilter
bool eventFilter(QObject *, QEvent *)
Definition: ktip.cpp:396
QLabel
QObject
QWidget
kDebug
#define kDebug
kaboutdata.h
kcomponentdata.h
kconfig.h
kdebug.h
kglobalsettings.h
klocale.h
i18n
QString i18n(const char *text)
i18nc
QString i18nc(const char *ctxt, const char *text)
kpushbutton.h
krandom.h
kseparator.h
kstandarddirs.h
ktextbrowser.h
ktip.h
KGlobal::mainComponent
const KComponentData & mainComponent()
KGlobal::dirs
KStandardDirs * dirs()
config
KSharedConfigPtr config()
KRandom::random
int random()
KStandardGuiItem::UseRTL
@ UseRTL
Definition: kstandardguiitem.h:46
KStandardGuiItem::back
KGuiItem back(BidiMode useBidi)
Returns the 'Back' gui item, like Konqueror's back button.
Definition: kstandardguiitem.cpp:206
KStandardGuiItem::close
KGuiItem close()
Returns the 'Close' gui item.
Definition: kstandardguiitem.cpp:182
KStandardGuiItem::forward
KGuiItem forward(BidiMode useBidi)
Returns the 'Forward' gui item, like Konqueror's forward button.
Definition: kstandardguiitem.cpp:214
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDEUI

Skip menu "KDEUI"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal