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

KDEUI

  • sources
  • kde-4.14
  • kdelibs
  • kdeui
  • dialogs
ktip.cpp
Go to the documentation of this file.
1 /*****************************************************************
2 
3 Copyright (c) 2000-2003 Matthias Hoelzer-Kluepfel <mhk@kde.org>
4  Tobias Koenig <tokoe@kde.org>
5  Daniel Molkentin <molkentin@kde.org>
6 Copyright (c) 2008 Urs Wolfer <uwolfer @ kde.org>
7 
8 Permission is hereby granted, free of charge, to any person obtaining a copy
9 of this software and associated documentation files (the "Software"), to deal
10 in the Software without restriction, including without limitation the rights
11 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 copies of the Software, and to permit persons to whom the Software is
13 furnished to do so, subject to the following conditions:
14 
15 The above copyright notice and this permission notice shall be included in
16 all copies or substantial portions of the Software.
17 
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 CONNECTION 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 
47 class 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 
57 void KTipDatabase::Private::loadTips( const QString &tipFile )
58 {
59  tips.clear();
60  addTips( tipFile );
61 }
62 
68 void 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 
115 KTipDatabase::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 
129 KTipDatabase::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 
143 KTipDatabase::~KTipDatabase()
144 {
145  delete d;
146 }
147 
148 void 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 
159 void 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 
170 QString KTipDatabase::tip() const
171 {
172  if ( d->tips.isEmpty() )
173  return QString();
174 
175  return d->tips[ d->currentTip ];
176 }
177 
178 
179 class 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 
203 KTipDialog *KTipDialog::Private::mInstance = 0;
204 
205 void 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 
212 void 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 
219 void KTipDialog::Private::_k_showOnStart( bool on )
220 {
221  parent->setShowOnStart( on );
222 }
223 
224 
225 KTipDialog::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 
329 KTipDialog::~KTipDialog()
330 {
331  if ( Private::mInstance == this )
332  Private::mInstance = 0L;
333  delete d;
334 }
335 
340 void KTipDialog::showTip( const QString &tipFile, bool force )
341 {
342  showTip( 0, tipFile, force );
343 }
344 
345 void KTipDialog::showTip( QWidget *parent, const QString &tipFile, bool force )
346 {
347  showMultiTip( parent, QStringList( tipFile ), force );
348 }
349 
350 void 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 
390 void KTipDialog::setShowOnStart( bool on )
391 {
392  KConfigGroup config( KGlobal::config(), "TipOfDay" );
393  config.writeEntry( "RunOnStart", on );
394 }
395 
396 bool 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"
i18n
QString i18n(const char *text)
ktextbrowser.h
QList::clear
void clear()
KPushButton
A QPushButton with drag-support and KGuiItem support.
Definition: kpushbutton.h:46
QString::indexOf
int indexOf(QChar ch, int from, Qt::CaseSensitivity cs) const
QEvent
QWidget
krandom.h
QEvent::type
Type type() const
QSize::width
int width() const
KStandardGuiItem::back
KGuiItem back(BidiMode useBidi)
Returns the 'Back' gui item, like Konqueror's back button.
Definition: kstandardguiitem.cpp:206
kdebug.h
KAboutData::appName
QString appName() const
QPalette::setColor
void setColor(ColorGroup group, ColorRole role, const QColor &color)
QByteArray
kglobalsettings.h
KTipDialog::~KTipDialog
~KTipDialog()
Destroys the tip dialog.
Definition: ktip.cpp:329
QFont
KStandardShortcut::label
QString label(StandardShortcut id)
Returns a localized label for user-visible display.
Definition: kstandardshortcut.cpp:267
KStandardDirs::locate
static QString locate(const char *type, const QString &filename, const KComponentData &cData=KGlobal::mainComponent())
KTipDialog::showTip
static void showTip(QWidget *parent, const QString &tipFile=QString(), bool force=false)
Shows a tip.
Definition: ktip.cpp:345
kconfig.h
QLabel::setPixmap
void setPixmap(const QPixmap &)
KTipDialog::setShowOnStart
static void setShowOnStart(bool show)
Toggles the start behavior.
Definition: ktip.cpp:390
KGlobal::dirs
KStandardDirs * dirs()
QHBoxLayout
KTipDatabase
A database for tips-of-the-day.
Definition: ktip.h:53
QLabel::setAlignment
void setAlignment(QFlags< Qt::AlignmentFlag >)
KConfigGroup::writeEntry
void writeEntry(const QString &key, const QVariant &value, WriteConfigFlags pFlags=Normal)
QRect::height
int height() const
KTipDialog::showMultiTip
static void showMultiTip(QWidget *parent, const QStringList &tipFiles, bool force=false)
Shows a tip.
Definition: ktip.cpp:350
QRect::x
int x() const
QRect::y
int y() const
KDialog::None
Definition: kdialog.h:138
KComponentData::aboutData
const KAboutData * aboutData() const
KDialog
A dialog base class with standard buttons and predefined layouts.
Definition: kdialog.h:128
kDebug
static QDebug kDebug(bool cond, int area=KDE_DEFAULT_DEBUG_AREA)
KTipDialog::eventFilter
bool eventFilter(QObject *, QEvent *)
Definition: ktip.cpp:396
klocale.h
QFile
KDialog::setCaption
virtual void setCaption(const QString &caption)
Make a KDE compliant caption.
Definition: kdialog.cpp:469
i18nc
QString i18nc(const char *ctxt, const char *text)
QWidget::setWindowIcon
void setWindowIcon(const QIcon &icon)
config
KSharedConfigPtr config()
KDialog::setMainWidget
void setMainWidget(QWidget *widget)
Sets the main widget of the dialog.
Definition: kdialog.cpp:338
KRandom::random
int random()
KSeparator
Standard horizontal or vertical separator.
Definition: kseparator.h:34
QWidget::resize
void resize(int w, int h)
QRegExp
QRect
QBoxLayout::addWidget
void addWidget(QWidget *widget, int stretch, QFlags< Qt::AlignmentFlag > alignment)
QList::count
int count(const T &value) const
KPushButton::setText
void setText(const QString &text)
Sets the text of the button.
Definition: kpushbutton.cpp:239
QString::fromUtf8
QString fromUtf8(const char *str, int size)
QObject
QWidget::setFocus
void setFocus()
QCheckBox
QList::isEmpty
bool isEmpty() const
QString::isEmpty
bool isEmpty() const
QByteArray::constData
const char * constData() const
QString::startsWith
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const
KIcon
A wrapper around QIcon that provides KDE icon features.
Definition: kicon.h:40
QWidget::move
void move(int x, int y)
QVBoxLayout
QString::endsWith
bool endsWith(const QString &s, Qt::CaseSensitivity cs) const
QObject::eventFilter
virtual bool eventFilter(QObject *watched, QEvent *event)
QList::first
T & first()
QLabel::setText
void setText(const QString &)
QString
KDialog::setButtons
void setButtons(ButtonCodes buttonMask)
Creates (or recreates) the button box and all the buttons in it.
Definition: kdialog.cpp:206
QLayout::setMargin
void setMargin(int margin)
KTipDatabase::KTipDatabase
KTipDatabase(const QString &tipFile=QString())
This constructor reads in the tips from a file with the given name.
Definition: ktip.cpp:115
QDialog::accept
virtual void accept()
QStringList
QWidget::rect
QRect rect() const
KStandardAction::next
KAction * next(const QObject *recvr, const char *slot, QObject *parent)
Scroll down one page.
Definition: kstandardaction.cpp:414
KStandardGuiItem::forward
KGuiItem forward(BidiMode useBidi)
Returns the 'Forward' gui item, like Konqueror's forward button.
Definition: kstandardguiitem.cpp:214
QList::end
iterator end()
QSize
QWidget::setFont
void setFont(const QFont &)
KStandardDirs::findResourceDir
QString findResourceDir(const char *type, const QString &filename) const
kpushbutton.h
KStandardDirs::resourceDirs
QStringList resourceDirs(const char *type) const
KTextBrowser
Extended QTextBrowser.
Definition: ktextbrowser.h:51
ktip.h
KStandardGuiItem::close
KGuiItem close()
Returns the 'Close' gui item.
Definition: kstandardguiitem.cpp:182
KStandardGuiItem::ok
KGuiItem ok()
Returns the 'Ok' gui item.
Definition: kstandardguiitem.cpp:107
kseparator.h
KStandardAction::replace
KAction * replace(const QObject *recvr, const char *slot, QObject *parent)
Find and replace matches.
Definition: kstandardaction.cpp:344
KConfigGroup
QKeyEvent
KTipDatabase::tip
QString tip() const
Returns the current tip.
Definition: ktip.cpp:170
KGlobalSettings::generalFont
static QFont generalFont()
Returns the default general font.
Definition: kglobalsettings.cpp:446
QRect::width
int width() const
QString::mid
QString mid(int position, int n) const
KTipDatabase::~KTipDatabase
~KTipDatabase()
Definition: ktip.cpp:143
KTipDialog::KTipDialog
KTipDialog(KTipDatabase *database, QWidget *parent=0)
Construct a tip dialog.
Definition: ktip.cpp:225
KGlobalSettings::splashScreenDesktopGeometry
static QRect splashScreenDesktopGeometry()
This function returns the desktop geometry for an application's splash screen.
Definition: kglobalsettings.cpp:713
kstandarddirs.h
QWidget::QWidget
QWidget(QWidget *parent, QFlags< Qt::WindowType > f)
QList::ConstIterator
typedef ConstIterator
QSize::height
int height() const
KStandardGuiItem::UseRTL
Definition: kstandardguiitem.h:46
KGlobal::mainComponent
const KComponentData & mainComponent()
QString::fromLatin1
QString fromLatin1(const char *str, int size)
KTipDatabase::prevTip
void prevTip()
The previous tip will become the current one.
Definition: ktip.cpp:159
kaboutdata.h
KTipDialog
A Tip-of-the-Day dialog.
Definition: ktip.h:103
kcomponentdata.h
QByteArray::size
int size() const
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QLabel
QObject::parent
QObject * parent() const
QPushButton::setDefault
void setDefault(bool)
KConfigGroup::readEntry
T readEntry(const QString &key, const T &aDefault) const
QList::begin
iterator begin()
QPalette
KTipDatabase::nextTip
void nextTip()
The next tip will become the current one.
Definition: ktip.cpp:148
QBoxLayout::addLayout
void addLayout(QLayout *layout, int stretch)
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:24:00 by doxygen 1.8.7 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 API Reference

Skip menu "kdelibs 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

Search



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