29 #include <QtCore/QFile>
30 #include <QtGui/QCheckBox>
31 #include <QtGui/QKeyEvent>
32 #include <QtGui/QLabel>
33 #include <QtGui/QLayout>
47 class KTipDatabase::Private
50 void loadTips(
const QString &tipFile );
51 void addTips(
const QString &tipFile );
57 void KTipDatabase::Private::loadTips(
const QString &tipFile )
68 void 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;
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 ];
179 class KTipDialog::Private
193 void _k_showOnStart(
bool );
197 QCheckBox *tipOnStart;
203 KTipDialog *KTipDialog::Private::mInstance = 0;
205 void KTipDialog::Private::_k_prevTip()
208 tipText->setHtml( QString::fromLatin1(
"<html><body>%1</body></html>" )
209 .arg(
i18n( database->tip().toUtf8() ) ) );
212 void KTipDialog::Private::_k_nextTip()
215 tipText->setHtml( QString::fromLatin1(
"<html><body>%1</body></html>" )
216 .arg(
i18n( database->tip().toUtf8() ) ) );
219 void 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();
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 );
QString i18n(const char *text)
KGuiItem back(BidiMode useBidi)
Returns the 'Back' gui item, like Konqueror's back button.
~KTipDialog()
Destroys the tip dialog.
QString label(StandardShortcut id)
Returns a localized label for user-visible display.
static QString locate(const char *type, const QString &filename, const KComponentData &cData=KGlobal::mainComponent())
static void showTip(QWidget *parent, const QString &tipFile=QString(), bool force=false)
Shows a tip.
static void setShowOnStart(bool show)
Toggles the start behavior.
A database for tips-of-the-day.
void writeEntry(const QString &key, const QVariant &value, WriteConfigFlags pFlags=Normal)
static void showMultiTip(QWidget *parent, const QStringList &tipFiles, bool force=false)
Shows a tip.
const KAboutData * aboutData() const
A dialog base class with standard buttons and predefined layouts.
static QDebug kDebug(bool cond, int area=KDE_DEFAULT_DEBUG_AREA)
bool eventFilter(QObject *, QEvent *)
virtual void setCaption(const QString &caption)
Make a KDE compliant caption.
QString i18nc(const char *ctxt, const char *text)
KSharedConfigPtr config()
void setMainWidget(QWidget *widget)
Sets the main widget of the dialog.
Standard horizontal or vertical separator.
A wrapper around QIcon that provides KDE icon features.
void setButtons(ButtonCodes buttonMask)
Creates (or recreates) the button box and all the buttons in it.
KTipDatabase(const QString &tipFile=QString())
This constructor reads in the tips from a file with the given name.
KAction * next(const QObject *recvr, const char *slot, QObject *parent)
Scroll down one page.
KGuiItem forward(BidiMode useBidi)
Returns the 'Forward' gui item, like Konqueror's forward button.
QString findResourceDir(const char *type, const QString &filename) const
QStringList resourceDirs(const char *type) const
KGuiItem close()
Returns the 'Close' gui item.
KGuiItem ok()
Returns the 'Ok' gui item.
KAction * replace(const QObject *recvr, const char *slot, QObject *parent)
Find and replace matches.
QString tip() const
Returns the current tip.
static QFont generalFont()
Returns the default general font.
KTipDialog(KTipDatabase *database, QWidget *parent=0)
Construct a tip dialog.
static QRect splashScreenDesktopGeometry()
This function returns the desktop geometry for an application's splash screen.
const KComponentData & mainComponent()
void prevTip()
The previous tip will become the current one.
T readEntry(const QString &key, const T &aDefault) const
void nextTip()
The next tip will become the current one.