29#include <QtCore/QFile>
30#include <QtGui/QCheckBox>
31#include <QtGui/QKeyEvent>
32#include <QtGui/QLabel>
33#include <QtGui/QLayout>
47class KTipDatabase::Private
50 void loadTips(
const QString &tipFile );
51 void addTips(
const QString &tipFile );
57void KTipDatabase::Private::loadTips(
const QString &tipFile )
68void KTipDatabase::Private::addTips(
const QString &tipFile )
72 if ( fileName.isEmpty() ) {
73 kDebug() <<
"KTipDatabase::addTips: can't find '" << tipFile <<
"' in standard dirs";
77 QFile file( fileName );
78 if ( !file.open( QIODevice::ReadOnly ) ) {
79 kDebug() <<
"KTipDatabase::addTips: can't open '" << fileName <<
"' for reading";
83 QByteArray data = file.readAll();
84 QString content = QString::fromUtf8( data.constData(), data.size() );
85 const QRegExp rx(
"\\n+" );
88 while ( ( pos = content.indexOf(
"<html>", pos + 1, Qt::CaseInsensitive ) ) != -1 ) {
94 .mid( pos + 6, content.indexOf(
"</html>", pos, Qt::CaseInsensitive ) - pos - 6 )
97 if ( !tip.endsWith(
'\n' ) )
100 if ( tip.startsWith(
'\n' ) )
103 if ( tip.isEmpty() ) {
104 kDebug() <<
"Empty tip found! Skipping! " << pos;
118 QString tipFile = _tipFile;
120 if ( tipFile.isEmpty() )
123 d->loadTips( tipFile );
125 if ( !d->tips.isEmpty() )
132 if ( tipsFiles.isEmpty() || ( ( tipsFiles.count() == 1 ) && tipsFiles.first().isEmpty() ) ) {
135 for ( QStringList::ConstIterator it = tipsFiles.begin(); it != tipsFiles.end(); ++it )
139 if ( !d->tips.isEmpty() )
150 if ( d->tips.isEmpty() )
155 if ( d->currentTip >= (
int) d->tips.count() )
161 if ( d->tips.isEmpty() )
166 if ( d->currentTip < 0 )
167 d->currentTip = d->tips.count() - 1;
172 if ( d->tips.isEmpty() )
175 return d->tips[ d->currentTip ];
179class KTipDialog::Private
193 void _k_showOnStart(
bool );
197 QCheckBox *tipOnStart;
203KTipDialog *KTipDialog::Private::mInstance = 0;
205void KTipDialog::Private::_k_prevTip()
208 tipText->setHtml( QString::fromLatin1(
"<html><body>%1</body></html>" )
209 .arg(
i18n( database->tip().toUtf8() ) ) );
212void KTipDialog::Private::_k_nextTip()
215 tipText->setHtml( QString::fromLatin1(
"<html><body>%1</body></html>" )
216 .arg(
i18n( database->tip().toUtf8() ) ) );
219void KTipDialog::Private::_k_showOnStart(
bool on )
227 d( new Private( this ) )
236 bool isTipDialog = (parent != 0);
238 d->database = database;
240 setWindowIcon(
KIcon(
"ktip"));
244 QVBoxLayout *mainLayout =
new QVBoxLayout( widget );
245 mainLayout->setMargin( 0 );
249 titleLabel->setText(
i18n(
"Did you know...?\n" ) );
251 titleLabel->setAlignment( Qt::AlignCenter );
252 mainLayout->addWidget( titleLabel );
255 QHBoxLayout *browserLayout =
new QHBoxLayout();
256 mainLayout->addLayout( browserLayout );
260 d->tipText->setOpenExternalLinks(
true );
262 d->tipText->setWordWrapMode( QTextOption::WrapAtWordBoundaryOrAnywhere );
268 d->tipText->setSearchPaths( paths );
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);
277 browserLayout->addWidget( d->tipText );
281 label->setAlignment( Qt::AlignRight | Qt::AlignVCenter );
282 browserLayout->addWidget( label );
284 if ( !isTipDialog ) {
290 move( rect.x() + ( rect.width() - sh.width() ) / 2,
291 rect.y() + ( rect.height() - sh.height() ) / 2 );
295 mainLayout->addWidget( sep );
297 QHBoxLayout *buttonLayout =
new QHBoxLayout();
299 mainLayout->addLayout( buttonLayout );
301 d->tipOnStart =
new QCheckBox(
i18n(
"&Show tips on startup" ) );
302 buttonLayout->addWidget( d->tipOnStart, 1 );
306 buttonLayout->addWidget( prev );
309 next->setText(
i18nc(
"Opposite to Previous",
"&Next" ) );
310 buttonLayout->addWidget( next );
313 ok->setDefault(
true );
314 buttonLayout->addWidget( ok );
317 d->tipOnStart->setChecked(
config.readEntry(
"RunOnStart",
true ) );
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)) );
331 if ( Private::mInstance ==
this )
332 Private::mInstance = 0L;
354 const bool runOnStart = configGroup.
readEntry(
"RunOnStart",
true );
379 if ( !Private::mInstance )
384 Private::mInstance->d->tipOnStart->setChecked( runOnStart );
386 Private::mInstance->show();
387 Private::mInstance->raise();
393 config.writeEntry(
"RunOnStart", on );
398 if (
object == d->tipText && event->type() == QEvent::KeyPress &&
399 (((QKeyEvent *)event)->key() == Qt::Key_Return ||
400 ((QKeyEvent *)event)->key() == Qt::Key_Space ))
411 return QWidget::eventFilter(
object, event );
const KAboutData * aboutData() const
QString readEntry(const char *key, const char *aDefault=0) const
A dialog base class with standard buttons and predefined layouts.
void setMainWidget(QWidget *widget)
Sets the main widget of the dialog.
void setButtons(ButtonCodes buttonMask)
Creates (or recreates) the button box and all the buttons in it.
virtual void setCaption(const QString &caption)
Make a KDE compliant caption.
static QFont generalFont()
Returns the default general font.
static QRect splashScreenDesktopGeometry()
This function returns the desktop geometry for an application's splash screen.
A wrapper around QIcon that provides KDE icon features.
Standard horizontal or vertical separator.
QString findResourceDir(const char *type, const QString &filename) const
static QString locate(const char *type, const QString &filename, const KComponentData &cData=KGlobal::mainComponent())
QStringList resourceDirs(const char *type) const
A database for tips-of-the-day.
void prevTip()
The previous tip will become the current one.
QString tip() const
Returns the current tip.
KTipDatabase(const QString &tipFile=QString())
This constructor reads in the tips from a file with the given name.
void nextTip()
The next tip will become the current one.
KTipDialog(KTipDatabase *database, QWidget *parent=0)
Construct a tip dialog.
static void setShowOnStart(bool show)
Toggles the start behavior.
~KTipDialog()
Destroys the tip dialog.
static void showMultiTip(QWidget *parent, const QStringList &tipFiles, bool force=false)
Shows a tip.
static void showTip(QWidget *parent, const QString &tipFile=QString(), bool force=false)
Shows a tip.
bool eventFilter(QObject *, QEvent *)
QString i18n(const char *text)
QString i18nc(const char *ctxt, const char *text)
const KComponentData & mainComponent()
KSharedConfigPtr config()
KGuiItem back(BidiMode useBidi)
Returns the 'Back' gui item, like Konqueror's back button.
KGuiItem close()
Returns the 'Close' gui item.
KGuiItem forward(BidiMode useBidi)
Returns the 'Forward' gui item, like Konqueror's forward button.