• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tdeui
 

tdeui

  • tdeui
ktip.cpp
1/*****************************************************************
2
3Copyright (c) 2000-2003 Matthias Hoelzer-Kluepfel <mhk@kde.org>
4 Tobias Koenig <tokoe@kde.org>
5 Daniel Molkentin <molkentin@kde.org>
6
7Permission is hereby granted, free of charge, to any person obtaining a copy
8of this software and associated documentation files (the "Software"), to deal
9in the Software without restriction, including without limitation the rights
10to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11copies of the Software, and to permit persons to whom the Software is
12furnished to do so, subject to the following conditions:
13
14The above copyright notice and this permission notice shall be included in
15all copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
24******************************************************************/
25
26#include <tqcheckbox.h>
27#include <tqfile.h>
28#include <tqhbox.h>
29#include <tqlabel.h>
30#include <tqlayout.h>
31#include <tqpushbutton.h>
32#include <tqregexp.h>
33#include <tqtextstream.h>
34#include <tqimage.h>
35
36#include <tdeaboutdata.h>
37#include <tdeapplication.h>
38#include <tdeconfig.h>
39#include <kdebug.h>
40#include <tdeglobal.h>
41#include <kiconloader.h>
42#include <tdelocale.h>
43#include <kpushbutton.h>
44#include <kseparator.h>
45#include <tdestandarddirs.h>
46#include <kstdguiitem.h>
47#include <ktextbrowser.h>
48#include <kiconeffect.h>
49#include <tdeglobalsettings.h>
50
51#ifdef TQ_WS_X11
52#include <twin.h>
53#endif
54
55#include "ktip.h"
56
57
58KTipDatabase::KTipDatabase(const TQString &_tipFile)
59{
60 TQString tipFile = _tipFile;
61 if (tipFile.isEmpty())
62 tipFile = TQString::fromLatin1(TDEGlobal::instance()->aboutData()->appName()) + "/tips";
63
64 loadTips(tipFile);
65
66 if (!mTips.isEmpty())
67 mCurrent = tdeApp->random() % mTips.count();
68}
69
70
71KTipDatabase::KTipDatabase( const TQStringList& tipsFiles )
72{
73 if ( tipsFiles.isEmpty() || ( ( tipsFiles.count() == 1 ) && tipsFiles.first().isEmpty() ) )
74 {
75 addTips(TQString::fromLatin1(TDEGlobal::instance()->aboutData()->appName()) + "/tips");
76 }
77 else
78 {
79 for (TQStringList::ConstIterator it = tipsFiles.begin(); it != tipsFiles.end(); ++it)
80 addTips( *it );
81 }
82 if (!mTips.isEmpty())
83 mCurrent = tdeApp->random() % mTips.count();
84
85}
86
87void KTipDatabase::loadTips(const TQString &tipFile)
88{
89 mTips.clear();
90 addTips(tipFile);
91}
92
93// if you change something here, please update the script
94// preparetips, which depends on extracting exactly the same
95// text as done here.
96void KTipDatabase::addTips(const TQString& tipFile )
97{
98 TQString fileName = locate("data", tipFile);
99
100 if (fileName.isEmpty())
101 {
102 kdDebug() << "KTipDatabase::addTips: can't find '" << tipFile << "' in standard dirs" << endl;
103 return;
104 }
105
106 TQFile file(fileName);
107 if (!file.open(IO_ReadOnly))
108 {
109 kdDebug() << "KTipDatabase::addTips: can't open '" << fileName << "' for reading" << endl;
110 return;
111 }
112
113 TQByteArray data = file.readAll();
114 TQString content = TQString::fromUtf8(data.data(), data.size());
115 const TQRegExp rx("\\n+");
116
117 int pos = -1;
118 while ((pos = content.find("<html>", pos + 1, false)) != -1)
119 {
120 // to make translations work, tip extraction here must exactly
121 // match what is done by the preparetips script
122 TQString tip = content
123 .mid(pos + 6, content.find("</html>", pos, false) - pos - 6)
124 .replace(rx, "\n");
125 if (!tip.endsWith("\n"))
126 tip += "\n";
127 if (tip.startsWith("\n"))
128 tip = tip.mid(1);
129 if (tip.isEmpty())
130 {
131 kdDebug() << "Empty tip found! Skipping! " << pos << endl;
132 continue;
133 }
134 mTips.append(tip);
135 }
136
137 file.close();
138
139}
140
141void KTipDatabase::nextTip()
142{
143 if (mTips.isEmpty())
144 return ;
145 mCurrent += 1;
146 if (mCurrent >= (int) mTips.count())
147 mCurrent = 0;
148}
149
150
151void KTipDatabase::prevTip()
152{
153 if (mTips.isEmpty())
154 return ;
155 mCurrent -= 1;
156 if (mCurrent < 0)
157 mCurrent = mTips.count() - 1;
158}
159
160
161TQString KTipDatabase::tip() const
162{
163 if (mTips.isEmpty())
164 return TQString::null;
165 return mTips[mCurrent];
166}
167
168KTipDialog *KTipDialog::mInstance = 0;
169
170
171KTipDialog::KTipDialog(KTipDatabase *db, TQWidget *parent, const char *name)
172 : KDialog(parent, name)
173{
178 bool isTipDialog = (parent);
179
180 TQImage img;
181 int h,s,v;
182
183 mBlendedColor = TDEGlobalSettings::activeTitleColor();
184 mBlendedColor.hsv(&h,&s,&v);
185 mBlendedColor.setHsv(h, int(s*(71/76.0)), int(v*(67/93.0)));
186
187 if (!isTipDialog)
188 {
189 img = TQImage(locate("data", "tdewizard/pics/wizard_small.png"));
190 // colorize and check to figure the correct color
191 TDEIconEffect::colorize(img, mBlendedColor, 1.0);
192 TQRgb colPixel( img.pixel(0,0) );
193
194 mBlendedColor = TQColor(tqRed(colPixel),tqGreen(colPixel),tqBlue(colPixel));
195 }
196
197 mBaseColor = TDEGlobalSettings::alternateBackgroundColor();
198 mBaseColor.hsv(&h,&s,&v);
199 mBaseColor.setHsv(h, int(s*(10/6.0)), int(v*(93/99.0)));
200
201 mTextColor = TDEGlobalSettings::textColor();
202
203
204 mDatabase = db;
205
206 setCaption(i18n("Tip of the Day"));
207#ifdef TQ_WS_X11
208 KWin::setIcons( winId(),
209 TDEGlobal::iconLoader()->loadIcon( "idea", TDEIcon::NoGroup, 32 ),
210 TDEGlobal::iconLoader()->loadIcon( "idea", TDEIcon::NoGroup, 16 ) );
211#endif
212 TQVBoxLayout *vbox = new TQVBoxLayout(this, marginHint(), spacingHint());
213
214 if (isTipDialog)
215 {
216 TQHBoxLayout *pl = new TQHBoxLayout(vbox, 0, 0);
217
218 TQLabel *bulb = new TQLabel(this);
219 bulb->setPixmap(locate("data", "tdeui/pics/ktip-bulb.png"));
220 pl->addWidget(bulb);
221
222 TQLabel *titlePane = new TQLabel(this);
223 titlePane->setBackgroundPixmap(locate("data", "tdeui/pics/ktip-background.png"));
224 titlePane->setText(i18n("Did you know...?\n"));
225 titlePane->setFont(TQFont(TDEGlobalSettings::generalFont().family(), 20, TQFont::Bold));
226 titlePane->setAlignment(TQLabel::AlignCenter);
227 pl->addWidget(titlePane, 100);
228 }
229
230 TQHBox *hbox = new TQHBox(this);
231 hbox->setSpacing(0);
232 hbox->setFrameStyle(TQFrame::Panel | TQFrame::Sunken);
233 vbox->addWidget(hbox);
234
235 TQHBox *tl = new TQHBox(hbox);
236 tl->setMargin(7);
237 tl->setBackgroundColor(mBlendedColor);
238
239 TQHBox *topLeft = new TQHBox(tl);
240 topLeft->setMargin(15);
241 topLeft->setBackgroundColor(mBaseColor);
242
243 mTipText = new KTextBrowser(topLeft);
244
245 mTipText->setWrapPolicy( TQTextEdit::AtWordOrDocumentBoundary );
246 mTipText->mimeSourceFactory()->addFilePath(
247 TDEGlobal::dirs()->findResourceDir("data", "tdewizard/pics")+"tdewizard/pics/");
248 mTipText->setFrameStyle(TQFrame::NoFrame | TQFrame::Plain);
249 mTipText->setHScrollBarMode(TQScrollView::AlwaysOff);
250 mTipText->setLinkUnderline(false);
251
252 TQStyleSheet *sheet = mTipText->styleSheet();
253 TQStyleSheetItem *item = sheet->item("a");
254 item->setFontWeight(TQFont::Bold);
255 mTipText->setStyleSheet(sheet);
256 TQPalette pal = mTipText->palette();
257 pal.setColor( TQPalette::Active, TQColorGroup::Link, mBlendedColor );
258 pal.setColor( TQPalette::Inactive, TQColorGroup::Link, mBlendedColor );
259 mTipText->setPalette(pal);
260
261 TQStringList icons = TDEGlobal::dirs()->resourceDirs("icon");
262 TQStringList::Iterator it;
263 for (it = icons.begin(); it != icons.end(); ++it)
264 mTipText->mimeSourceFactory()->addFilePath(*it);
265
266 if (!isTipDialog)
267 {
268 TQLabel *l = new TQLabel(hbox);
269 l->setPixmap(img);
270 l->setBackgroundColor(mBlendedColor);
271 l->setAlignment(TQt::AlignRight | TQt::AlignBottom);
272
273 resize(550, 230);
274 TQSize sh = size();
275
276 TQRect rect = TDEGlobalSettings::splashScreenDesktopGeometry();
277
278 move(rect.x() + (rect.width() - sh.width())/2,
279 rect.y() + (rect.height() - sh.height())/2);
280 }
281
282 KSeparator* sep = new KSeparator( KSeparator::HLine, this);
283 vbox->addWidget(sep);
284
285 TQHBoxLayout *hbox2 = new TQHBoxLayout(vbox, 4);
286
287 mTipOnStart = new TQCheckBox(i18n("&Show tips on startup"), this);
288 hbox2->addWidget(mTipOnStart, 1);
289
290 KPushButton *prev = new KPushButton( KStdGuiItem::back(
291 KStdGuiItem::UseRTL ), this );
292 prev->setText( i18n("&Previous") );
293 hbox2->addWidget(prev);
294
295 KPushButton *next = new KPushButton( KStdGuiItem::forward(
296 KStdGuiItem::UseRTL ), this );
297 next->setText( i18n("Opposite to Previous","&Next") );
298 hbox2->addWidget(next);
299
300 KPushButton *ok = new KPushButton(KStdGuiItem::close(), this);
301 ok->setDefault(true);
302 hbox2->addWidget(ok);
303
304 TDEConfigGroup config(tdeApp->config(), "TipOfDay");
305 mTipOnStart->setChecked(config.readBoolEntry("RunOnStart", true));
306
307 connect(next, TQ_SIGNAL(clicked()), this, TQ_SLOT(nextTip()));
308 connect(prev, TQ_SIGNAL(clicked()), this, TQ_SLOT(prevTip()));
309 connect(ok, TQ_SIGNAL(clicked()), this, TQ_SLOT(accept()));
310 connect(mTipOnStart, TQ_SIGNAL(toggled(bool)), this, TQ_SLOT(showOnStart(bool)));
311
312 ok->setFocus();
313
314 nextTip();
315}
316
317KTipDialog::~KTipDialog()
318{
319 if( mInstance==this )
320 mInstance = 0L;
321}
322
323void KTipDialog::showTip(const TQString &tipFile, bool force)
324{
325 showTip(tdeApp->mainWidget(), tipFile, force);
326}
327
328void KTipDialog::showTip(TQWidget *parent, const TQString &tipFile, bool force)
329{
330 showMultiTip( parent, TQStringList(tipFile), force );
331}
332
333void KTipDialog::showMultiTip(TQWidget *parent, const TQStringList &tipFiles, bool force)
334{
335 TDEConfigGroup configGroup(tdeApp->config(), "TipOfDay");
336
337 const bool runOnStart = configGroup.readBoolEntry("RunOnStart", true);
338
339 if (!force)
340 {
341 if (!runOnStart)
342 return;
343
344 bool hasLastShown = configGroup.hasKey("TipLastShown");
345 if (hasLastShown)
346 {
347 const int oneDay = 24*60*60;
348 TQDateTime lastShown = configGroup.readDateTimeEntry("TipLastShown");
349 // Show tip roughly once a week
350 if (lastShown.secsTo(TQDateTime::currentDateTime()) < (oneDay + (tdeApp->random() % (10*oneDay))))
351 return;
352 }
353 configGroup.writeEntry("TipLastShown", TQDateTime::currentDateTime());
354 tdeApp->config()->sync();
355 if (!hasLastShown)
356 return; // Don't show tip on first start
357 }
358
359 if (!mInstance)
360 mInstance = new KTipDialog(new KTipDatabase(tipFiles), parent);
361 else
362 // The application might have changed the RunOnStart option in its own
363 // configuration dialog, so we should update the checkbox.
364 mInstance->mTipOnStart->setChecked(runOnStart);
365
366 mInstance->show();
367 mInstance->raise();
368 }
369
370static TQString fixTip(TQString tip)
371{
372 TQRegExp iconRegExp("<img src=\"(.*)\">");
373 iconRegExp.setMinimal(true);
374 if (iconRegExp.search(tip)>-1) {
375 TQString iconName = iconRegExp.cap(1);
376 if (!iconName.isEmpty())
377 if (TDEGlobal::dirs()->findResource("icon", iconName).isEmpty())
378 tip.replace("crystalsvg","hicolor");
379 }
380
381 return tip;
382}
383
384 void KTipDialog::prevTip()
385 {
386 mDatabase->prevTip();
387 TQString currentTip = TQString::fromLatin1(
388 "<qt text=\"%1\" bgcolor=\"%2\">%3</qt>")
389 .arg(mTextColor.name())
390 .arg(mBaseColor.name())
391 .arg(i18n(mDatabase->tip().utf8()));
392
393
394 currentTip = fixTip(currentTip);
395 mTipText->setText(currentTip);
396 mTipText->setContentsPos(0, 0);
397 }
398
399 void KTipDialog::nextTip()
400 {
401 mDatabase->nextTip();
402 TQString currentTip = TQString::fromLatin1(
403 "<qt text=\"%1\" bgcolor=\"%2\">%3</qt>")
404 .arg(mTextColor.name())
405 .arg(mBaseColor.name())
406 .arg(i18n(mDatabase->tip().utf8()));
407
408
409 currentTip = fixTip(currentTip);
410 mTipText->setText(currentTip);
411 mTipText->setContentsPos(0, 0);
412 }
413
414 void KTipDialog::showOnStart(bool on)
415 {
416 setShowOnStart(on);
417 }
418
419 void KTipDialog::setShowOnStart(bool on)
420 {
421 TDEConfigGroup config(tdeApp->config(), "TipOfDay");
422 config.writeEntry("RunOnStart", on);
423 config.sync();
424 }
425
426 bool KTipDialog::eventFilter(TQObject *o, TQEvent *e)
427 {
428 if (o == mTipText && e->type()== TQEvent::KeyPress &&
429 (((TQKeyEvent *)e)->key() == Key_Return ||
430 ((TQKeyEvent *)e)->key() == Key_Space ))
431 accept();
432
433 // If the user presses Return or Space, we close the dialog as if the
434 // default button was pressed even if the KTextBrowser has the keyboard
435 // focus. This could have the bad side-effect that the user cannot use the
436 // keyboard to open urls in the KTextBrowser, so we just let it handle
437 // the key event _additionally_. (Antonio)
438
439 return TQWidget::eventFilter( o, e );
440}
441
442void KTipDialog::virtual_hook( int id, void* data )
443{
444 KDialog::virtual_hook( id, data );
445}
446
447#include "ktip.moc"
KDialog
Dialog with extended non-modal support and methods for KDE standard compliance.
Definition: kdialog.h:53
KDialog::marginHint
static int marginHint()
Return the number of pixels you shall use between a dialog edge and the outermost widget(s) according...
Definition: kdialog.cpp:104
KDialog::spacingHint
static int spacingHint()
Return the number of pixels you shall use between widgets inside a dialog according to the KDE standa...
Definition: kdialog.cpp:110
KDialog::setCaption
virtual void setCaption(const TQString &caption)
Make a KDE compliant caption.
Definition: kdialog.cpp:122
KPushButton
This is nothing but a TQPushButton with drag-support and KGuiItem support.
Definition: kpushbutton.h:38
KPushButton::setText
void setText(const TQString &text)
Sets the text of the button.
Definition: kpushbutton.cpp:148
KSeparator
Standard horizontal or vertical separator.
Definition: kseparator.h:34
KStdGuiItem::back
static KGuiItem back(BidiMode useBidi=IgnoreRTL)
Return a GUI item for a 'back' action, like Konqueror's back button.
Definition: kstdguiitem.cpp:184
KStdGuiItem::forward
static KGuiItem forward(BidiMode useBidi=IgnoreRTL)
Return a GUI item for a 'forward' action, like Konqueror's forward button.
Definition: kstdguiitem.cpp:192
KTextBrowser
Extended TQTextBrowser.
Definition: ktextbrowser.h:43
KTipDatabase
A database for tips-of-the-day.
Definition: ktip.h:56
KTipDatabase::KTipDatabase
KTipDatabase(const TQString &tipFile=TQString::null)
This constructor reads in the tips from a file with the given name.
Definition: ktip.cpp:58
KTipDatabase::prevTip
void prevTip()
The previous tip will become the current one.
Definition: ktip.cpp:151
KTipDatabase::tip
TQString tip() const
Returns the current tip.
Definition: ktip.cpp:161
KTipDatabase::nextTip
void nextTip()
The next tip will become the current one.
Definition: ktip.cpp:141
KTipDialog
A Tip-of-the-Day dialog.
Definition: ktip.h:109
KTipDialog::KTipDialog
KTipDialog(KTipDatabase *db, TQWidget *parent=0, const char *name=0)
Construct a tip dialog.
Definition: ktip.cpp:171
KTipDialog::showTip
static void showTip(TQWidget *parent, const TQString &tipFile=TQString::null, bool force=false)
Shows a tip.
Definition: ktip.cpp:328
KTipDialog::setShowOnStart
static void setShowOnStart(bool show)
Toggles the start behavior.
Definition: ktip.cpp:419
KTipDialog::showMultiTip
static void showMultiTip(TQWidget *parent, const TQStringList &tipFiles, bool force=false)
Shows a tip.
Definition: ktip.cpp:333
KWin::setIcons
static void setIcons(WId win, const TQPixmap &icon, const TQPixmap &miniIcon)
TDEConfigBase::readBoolEntry
bool readBoolEntry(const TQString &pKey, bool bDefault=false) const
TDEConfigBase::hasKey
bool hasKey(const TQString &key) const
TDEConfigBase::writeEntry
void writeEntry(const TQString &pKey, const TQString &pValue, bool bPersistent=true, bool bGlobal=false, bool bNLS=false)
TDEConfigBase::readDateTimeEntry
TQDateTime readDateTimeEntry(const TQString &pKey, const TQDateTime *pDefault=0L) const
TDEConfigGroup
TDEConfigGroup::sync
virtual void sync()
TDEGlobalSettings::generalFont
static TQFont generalFont()
TDEGlobalSettings::alternateBackgroundColor
static TQColor alternateBackgroundColor()
TDEGlobalSettings::activeTitleColor
static TQColor activeTitleColor()
TDEGlobalSettings::textColor
static TQColor textColor()
TDEGlobalSettings::splashScreenDesktopGeometry
static TQRect splashScreenDesktopGeometry()
TDEGlobal::iconLoader
static TDEIconLoader * iconLoader()
TDEGlobal::dirs
static TDEStandardDirs * dirs()
TDEGlobal::instance
static TDEInstance * instance()
TDEIconEffect::colorize
static void colorize(TQImage &image, const TQColor &col, float value)
TDEIcon::NoGroup
NoGroup
TDEStandardDirs::resourceDirs
TQStringList resourceDirs(const char *type) const
endl
kndbgstream & endl(kndbgstream &s)
kdDebug
kdbgstream kdDebug(int area=0)
locate
TQString locate(const char *type, const TQString &filename, const TDEInstance *instance=TDEGlobal::instance())
tdelocale.h

tdeui

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

tdeui

Skip menu "tdeui"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdeui by doxygen 1.9.4
This website is maintained by Timothy Pearson.