kdecore Library API Documentation

kglobalsettings.cpp

00001 /* This file is part of the KDE libraries
00002    Copyright (C) 2000 David Faure <faure@kde.org>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License version 2 as published by the Free Software Foundation.
00007 
00008    This library is distributed in the hope that it will be useful,
00009    but WITHOUT ANY WARRANTY; without even the implied warranty of
00010    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011    Library General Public License for more details.
00012 
00013    You should have received a copy of the GNU Library General Public License
00014    along with this library; see the file COPYING.LIB.  If not, write to
00015    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00016    Boston, MA 02111-1307, USA.
00017 */
00018 
00019 #include "kglobalsettings.h"
00020 
00021 #include <qdir.h>
00022 #include <qpixmap.h>
00023 #include <qfontdatabase.h>
00024 
00025 #include <kconfig.h>
00026 #include <ksimpleconfig.h>
00027 #include <kapplication.h>
00028 #include <kipc.h>
00029 #include <kdebug.h>
00030 #include <kglobal.h>
00031 #include <kshortcut.h>
00032 #include <kstandarddirs.h>
00033 #include <kcharsets.h>
00034 #include <kaccel.h>
00035 #include <klocale.h>
00036 #include <qfontinfo.h>
00037 #include <stdlib.h>
00038 
00039 #ifdef Q_WS_X11
00040 //#include <X11/X.h>
00041 #include <X11/Xlib.h>
00042 //#include <X11/Xutil.h>
00043 #endif
00044 
00045 QString* KGlobalSettings::s_desktopPath = 0;
00046 QString* KGlobalSettings::s_autostartPath = 0;
00047 QString* KGlobalSettings::s_trashPath = 0;
00048 QString* KGlobalSettings::s_documentPath = 0;
00049 QFont *KGlobalSettings::_generalFont = 0;
00050 QFont *KGlobalSettings::_fixedFont = 0;
00051 QFont *KGlobalSettings::_toolBarFont = 0;
00052 QFont *KGlobalSettings::_menuFont = 0;
00053 QFont *KGlobalSettings::_windowTitleFont = 0;
00054 QFont *KGlobalSettings::_taskbarFont = 0;
00055 QFont *KGlobalSettings::_largeFont = 0;
00056 QColor *KGlobalSettings::kde2Blue = 0;
00057 QColor *KGlobalSettings::kde2Gray = 0;
00058 QColor *KGlobalSettings::kde2AlternateColor = 0;
00059 
00060 KGlobalSettings::KMouseSettings *KGlobalSettings::s_mouseSettings = 0;
00061 
00062 int KGlobalSettings::dndEventDelay()
00063 {
00064     KConfig *c = KGlobal::config();
00065     KConfigGroupSaver cgs( c, "General" );
00066     return c->readNumEntry("StartDragDist", QApplication::startDragDistance());
00067 }
00068 
00069 bool KGlobalSettings::singleClick()
00070 {
00071     KConfig *c = KGlobal::config();
00072     KConfigGroupSaver cgs( c, "KDE" );
00073     return c->readBoolEntry("SingleClick", KDE_DEFAULT_SINGLECLICK);
00074 }
00075 
00076 KGlobalSettings::TearOffHandle KGlobalSettings::insertTearOffHandle()
00077 {
00078     int tearoff;
00079     KConfig *c = KGlobal::config();
00080     KConfigGroupSaver cgs( c, "KDE" );
00081     tearoff =c->readNumEntry("InsertTearOffHandle", KDE_DEFAULT_INSERTTEAROFFHANDLES);
00082     return (TearOffHandle) tearoff;
00083 }
00084 
00085 bool KGlobalSettings::changeCursorOverIcon()
00086 {
00087     KConfig *c = KGlobal::config();
00088     KConfigGroupSaver cgs( c, "KDE" );
00089     return c->readBoolEntry("ChangeCursor", KDE_DEFAULT_CHANGECURSOR);
00090 }
00091 
00092 bool KGlobalSettings::visualActivate()
00093 {
00094     KConfig *c = KGlobal::config();
00095     KConfigGroupSaver cgs( c, "KDE" );
00096     return c->readBoolEntry("VisualActivate", KDE_DEFAULT_VISUAL_ACTIVATE);
00097 }
00098 
00099 unsigned int KGlobalSettings::visualActivateSpeed()
00100 {
00101     KConfig *c = KGlobal::config();
00102     KConfigGroupSaver cgs( c, "KDE" );
00103     return
00104         c->readNumEntry(
00105             "VisualActivateSpeed",
00106             KDE_DEFAULT_VISUAL_ACTIVATE_SPEED
00107         );
00108 }
00109 
00110 
00111 
00112 int KGlobalSettings::autoSelectDelay()
00113 {
00114     KConfig *c = KGlobal::config();
00115     KConfigGroupSaver cgs( c, "KDE" );
00116     return c->readNumEntry("AutoSelectDelay", KDE_DEFAULT_AUTOSELECTDELAY);
00117 }
00118 
00119 KGlobalSettings::Completion KGlobalSettings::completionMode()
00120 {
00121     int completion;
00122     KConfig *c = KGlobal::config();
00123     KConfigGroupSaver cgs( c, "General" );
00124     completion = c->readNumEntry("completionMode", -1);
00125     if ((completion < (int) CompletionNone) ||
00126         (completion > (int) CompletionPopupAuto))
00127       {
00128         completion = (int) CompletionPopup; // Default
00129       }
00130   return (Completion) completion;
00131 }
00132 
00133 bool KGlobalSettings::showContextMenusOnPress ()
00134 {
00135     KConfig *c = KGlobal::config();
00136     KConfigGroupSaver cgs (c, "ContextMenus");
00137 
00138     return cgs.config()->readBoolEntry("ShowOnPress", true);
00139 }
00140 
00141 int KGlobalSettings::contextMenuKey ()
00142 {
00143     KConfig *c = KGlobal::config();
00144     KConfigGroupSaver cgs (c, "Shortcuts");
00145 
00146     KShortcut cut (cgs.config()->readEntry ("PopupMenuContext", "Menu"));
00147     return cut.keyCodeQt();
00148 }
00149 
00150 QColor KGlobalSettings::toolBarHighlightColor()
00151 {
00152     initColors();
00153     KConfig *c = KGlobal::config();
00154     KConfigGroupSaver cgs( c, QString::fromLatin1("Toolbar style") );
00155     return c->readColorEntry("HighlightColor", kde2Blue);
00156 }
00157 
00158 QColor KGlobalSettings::inactiveTitleColor()
00159 {
00160     if (!kde2Gray)
00161         kde2Gray = new QColor(220, 220, 220);
00162     KConfig *c = KGlobal::config();
00163     KConfigGroupSaver cgs( c, QString::fromLatin1("WM") );
00164     return c->readColorEntry( "inactiveBackground", kde2Gray );
00165 }
00166 
00167 QColor KGlobalSettings::inactiveTextColor()
00168 {
00169     KConfig *c = KGlobal::config();
00170     KConfigGroupSaver cgs( c, QString::fromLatin1("WM") );
00171     return c->readColorEntry( "inactiveForeground", &Qt::darkGray );
00172 }
00173 
00174 QColor KGlobalSettings::activeTitleColor()
00175 {
00176     initColors();
00177     KConfig *c = KGlobal::config();
00178     KConfigGroupSaver cgs( c, QString::fromLatin1("WM") );
00179     return c->readColorEntry( "activeBackground", kde2Blue);
00180 }
00181 
00182 QColor KGlobalSettings::activeTextColor()
00183 {
00184     KConfig *c = KGlobal::config();
00185     KConfigGroupSaver cgs( c, QString::fromLatin1("WM") );
00186     return c->readColorEntry( "activeForeground", &Qt::white );
00187 }
00188 
00189 int KGlobalSettings::contrast()
00190 {
00191     KConfig *c = KGlobal::config();
00192     KConfigGroupSaver cgs( c, QString::fromLatin1("KDE") );
00193     return c->readNumEntry( "contrast", 7 );
00194 }
00195 
00196 // IMPORTANT:
00197 //  This function should be get in sync with
00198 //   KApplication::kdisplaySetPalette()
00199 QColor KGlobalSettings::baseColor()
00200 {
00201     KConfig *c = KGlobal::config();
00202     KConfigGroupSaver cgs( c, QString::fromLatin1("General") );
00203     return c->readColorEntry( "windowBackground", &Qt::white );
00204 }
00205 
00206 // IMPORTANT:
00207 //  This function should be get in sync with
00208 //   KApplication::kdisplaySetPalette()
00209 QColor KGlobalSettings::textColor()
00210 {
00211     KConfig *c = KGlobal::config();
00212     KConfigGroupSaver cgs( c, QString::fromLatin1("General") );
00213     return c->readColorEntry( "windowForeground", &Qt::black );
00214 }
00215 
00216 // IMPORTANT:
00217 //  This function should be get in sync with
00218 //   KApplication::kdisplaySetPalette()
00219 QColor KGlobalSettings::highlightedTextColor()
00220 {
00221     KConfig *c = KGlobal::config();
00222     KConfigGroupSaver cgs( c, QString::fromLatin1("General") );
00223     return c->readColorEntry( "selectForeground", &Qt::white );
00224 }
00225 
00226 // IMPORTANT:
00227 //  This function should be get in sync with
00228 //   KApplication::kdisplaySetPalette()
00229 QColor KGlobalSettings::highlightColor()
00230 {
00231     initColors();
00232     KConfig *c = KGlobal::config();
00233     KConfigGroupSaver cgs( c, QString::fromLatin1("General") );
00234     return c->readColorEntry( "selectBackground", kde2Blue );
00235 }
00236 
00237 QColor KGlobalSettings::alternateBackgroundColor()
00238 {
00239     initColors();
00240     KConfig *c = KGlobal::config();
00241     KConfigGroupSaver cgs( c, QString::fromLatin1("General") );
00242     *kde2AlternateColor = calculateAlternateBackgroundColor( baseColor() );
00243     return c->readColorEntry( "alternateBackground", kde2AlternateColor );
00244 }
00245 
00246 QColor KGlobalSettings::calculateAlternateBackgroundColor(const QColor& base)
00247 {
00248     if (base == Qt::white)
00249         return QColor(238,246,255);
00250     else
00251     {
00252         int h, s, v;
00253         base.hsv( &h, &s, &v );
00254         if (v > 128)
00255             return base.dark(106);
00256         else if (base != Qt::black)
00257             return base.light(110);
00258 
00259         return QColor(32,32,32);
00260     }
00261 }
00262 
00263 QColor KGlobalSettings::linkColor()
00264 {
00265     initColors();
00266     KConfig *c = KGlobal::config();
00267     KConfigGroupSaver cgs( c, QString::fromLatin1("General") );
00268     return c->readColorEntry( "linkColor", kde2Blue );
00269 }
00270 
00271 QColor KGlobalSettings::visitedLinkColor()
00272 {
00273     KConfig *c = KGlobal::config();
00274     KConfigGroupSaver cgs( c, QString::fromLatin1("General") );
00275     return c->readColorEntry( "visitedLinkColor", &Qt::magenta );
00276 }
00277 
00278 QFont KGlobalSettings::generalFont()
00279 {
00280     if (_generalFont)
00281         return *_generalFont;
00282 
00283     _generalFont = new QFont("helvetica", 12);
00284     _generalFont->setPixelSize(12);
00285     _generalFont->setStyleHint(QFont::SansSerif);
00286 
00287     KConfig *c = KGlobal::config();
00288     KConfigGroupSaver cgs( c, QString::fromLatin1("General") );
00289     *_generalFont = c->readFontEntry("font", _generalFont);
00290 
00291     return *_generalFont;
00292 }
00293 
00294 QFont KGlobalSettings::fixedFont()
00295 {
00296     if (_fixedFont)
00297         return *_fixedFont;
00298 
00299     _fixedFont = new QFont("courier", 12);
00300     _fixedFont->setPixelSize(12);
00301     _fixedFont->setStyleHint(QFont::TypeWriter);
00302 
00303     KConfig *c = KGlobal::config();
00304     KConfigGroupSaver cgs( c, QString::fromLatin1("General") );
00305     *_fixedFont = c->readFontEntry("fixed", _fixedFont);
00306 
00307     return *_fixedFont;
00308 }
00309 
00310 QFont KGlobalSettings::toolBarFont()
00311 {
00312     if(_toolBarFont)
00313         return *_toolBarFont;
00314 
00315     _toolBarFont = new QFont("helvetica", 10);
00316     _toolBarFont->setPixelSize(10);
00317     _toolBarFont->setStyleHint(QFont::SansSerif);
00318 
00319     KConfig *c = KGlobal::config();
00320     KConfigGroupSaver cgs( c, QString::fromLatin1("General") );
00321     *_toolBarFont = c->readFontEntry("toolBarFont", _toolBarFont);
00322 
00323     return *_toolBarFont;
00324 }
00325 
00326 QFont KGlobalSettings::menuFont()
00327 {
00328     if(_menuFont)
00329         return *_menuFont;
00330 
00331     _menuFont = new QFont("helvetica", 12);
00332     _menuFont->setPixelSize(12);
00333     _menuFont->setStyleHint(QFont::SansSerif);
00334 
00335     KConfig *c = KGlobal::config();
00336     KConfigGroupSaver cgs( c, QString::fromLatin1("General") );
00337     *_menuFont = c->readFontEntry("menuFont", _menuFont);
00338 
00339     return *_menuFont;
00340 }
00341 
00342 QFont KGlobalSettings::windowTitleFont()
00343 {
00344     if(_windowTitleFont)
00345         return *_windowTitleFont;
00346 
00347     _windowTitleFont = new QFont("helvetica", 12, QFont::Bold);
00348     _windowTitleFont->setPixelSize(12);
00349     _windowTitleFont->setStyleHint(QFont::SansSerif);
00350 
00351     KConfig *c = KGlobal::config();
00352     KConfigGroupSaver cgs( c, QString::fromLatin1("WM") );
00353     *_windowTitleFont = c->readFontEntry("activeFont", _windowTitleFont); // inconsistency
00354 
00355     return *_windowTitleFont;
00356 }
00357 
00358 QFont KGlobalSettings::taskbarFont()
00359 {
00360     if(_taskbarFont)
00361         return *_taskbarFont;
00362 
00363     _taskbarFont = new QFont("helvetica", 11);
00364     _taskbarFont->setPixelSize(11);
00365     _taskbarFont->setStyleHint(QFont::SansSerif);
00366 
00367     KConfig *c = KGlobal::config();
00368     KConfigGroupSaver cgs( c, QString::fromLatin1("General") );
00369     *_taskbarFont = c->readFontEntry("taskbarFont", _taskbarFont);
00370 
00371     return *_taskbarFont;
00372 }
00373 
00374 
00375 QFont KGlobalSettings::largeFont(const QString &text)
00376 {
00377     QFontDatabase db;
00378     QStringList fam = db.families();
00379         
00380     // Move a bunch of preferred fonts to the front.
00381     if (fam.remove("Arial"))
00382        fam.prepend("Arial");
00383     if (fam.remove("Verdana"))
00384        fam.prepend("Verdana");
00385     if (fam.remove("Tahoma"))
00386        fam.prepend("Tahoma");
00387     if (fam.remove("Lucida Sans"))
00388        fam.prepend("Lucida Sans");
00389     if (fam.remove("Lucidux Sans"))
00390        fam.prepend("Lucidux Sans");
00391     if (fam.remove("Nimbus Sans"))
00392        fam.prepend("Nimbus Sans");
00393     if (fam.remove("Gothic I"))
00394        fam.prepend("Gothic I");
00395 
00396     if (_largeFont)
00397         fam.prepend(_largeFont->family());
00398 
00399     for(QStringList::ConstIterator it = fam.begin();
00400         it != fam.end(); ++it)
00401     {
00402         if (db.isSmoothlyScalable(*it) && !db.isFixedPitch(*it))
00403         {
00404             QFont font(*it);
00405             font.setPixelSize(75);
00406             QFontMetrics metrics(font);
00407             int h = metrics.height();
00408             if ((h < 60) || ( h > 90))
00409                 continue;
00410 
00411             bool ok = true;
00412             for(unsigned int i = 0; i < text.length(); i++)
00413             {
00414                 if (!metrics.inFont(text[i]))
00415                 {
00416                     ok = false;
00417                     break;
00418                 }
00419             }
00420             if (!ok)
00421                 continue;
00422 
00423             font.setPointSize(48);
00424             _largeFont = new QFont(font);
00425             return *_largeFont;
00426         }
00427     }
00428     _largeFont = new QFont(KGlobalSettings::generalFont());
00429     _largeFont->setPointSize(48);
00430     return *_largeFont;
00431 }
00432 
00433 void KGlobalSettings::initStatic() // should be called initPaths(). Don't put anything else here.
00434 {
00435     if ( s_desktopPath != 0 )
00436         return;
00437 
00438     s_desktopPath = new QString();
00439     s_autostartPath = new QString();
00440     s_trashPath = new QString();
00441     s_documentPath = new QString();
00442 
00443     KConfig *config = KGlobal::config();
00444     bool dollarExpansion = config->isDollarExpansion();
00445     config->setDollarExpansion(true);
00446     KConfigGroupSaver cgs( config, "Paths" );
00447 
00448     // Desktop Path
00449     *s_desktopPath = QDir::homeDirPath() + "/" + "Desktop" + "/";
00450     *s_desktopPath = config->readPathEntry( "Desktop", *s_desktopPath);
00451     if ( (*s_desktopPath)[0] != '/' )
00452       s_desktopPath->prepend( QDir::homeDirPath() + "/" );
00453     *s_desktopPath = QDir::cleanDirPath( *s_desktopPath );
00454     if ( s_desktopPath->right(1) != "/")
00455         *s_desktopPath += "/";
00456 
00457     // Trash Path
00458     *s_trashPath = *s_desktopPath + i18n("Trash") + "/";
00459     *s_trashPath = config->readPathEntry( "Trash" , *s_trashPath);
00460     if ( (*s_trashPath)[0] != '/' )
00461       s_trashPath->prepend( QDir::homeDirPath() + "/" );
00462     *s_trashPath = QDir::cleanDirPath( *s_trashPath );
00463     if ( s_trashPath->right(1) != "/")
00464         *s_trashPath += "/";
00465     // We need to save it in any case, in case the language changes later on,
00466     if ( !config->hasKey( "Trash" ) )
00467     {
00468       config->writePathEntry( "Trash", *s_trashPath, true, true );
00469       config->sync();
00470     }
00471 
00472     // Autostart Path
00473     *s_autostartPath = KGlobal::dirs()->localkdedir() + "Autostart" + "/";
00474     *s_autostartPath = config->readPathEntry( "Autostart" , *s_autostartPath);
00475     if ( (*s_autostartPath)[0] != '/' )
00476       s_autostartPath->prepend( QDir::homeDirPath() + "/" );
00477     *s_autostartPath = QDir::cleanDirPath( *s_autostartPath );
00478     if ( s_autostartPath->right(1) != "/")
00479         *s_autostartPath += "/";
00480 
00481     // Document Path
00482     *s_documentPath = QString::null;
00483     *s_documentPath = config->readPathEntry( "Documents" , *s_documentPath);
00484     if ( (*s_documentPath)[0] != '/' )
00485       s_documentPath->prepend( QDir::homeDirPath() + "/" );
00486     *s_documentPath = QDir::cleanDirPath( *s_documentPath );
00487     if ( s_documentPath->right(1) != "/")
00488         *s_documentPath += "/";
00489 
00490     config->setDollarExpansion(dollarExpansion);
00491 
00492     // Make sure this app gets the notifications about those paths
00493     if (kapp)
00494         kapp->addKipcEventMask(KIPC::SettingsChanged);
00495 }
00496 
00497 void KGlobalSettings::initColors()
00498 {
00499     if (!kde2Blue) {
00500       if (QPixmap::defaultDepth() > 8)
00501         kde2Blue = new QColor(84, 112, 152);
00502       else
00503         kde2Blue = new QColor(0, 0, 192);
00504     }
00505     if (!kde2AlternateColor)
00506       kde2AlternateColor = new QColor(240, 240, 240);
00507 }
00508 
00509 void KGlobalSettings::rereadFontSettings()
00510 {
00511     delete _generalFont;
00512     _generalFont = 0L;
00513     delete _fixedFont;
00514     _fixedFont = 0L;
00515     delete _menuFont;
00516     _menuFont = 0L;
00517     delete _toolBarFont;
00518     _toolBarFont = 0L;
00519     delete _windowTitleFont;
00520     _windowTitleFont = 0L;
00521     delete _taskbarFont;
00522     _taskbarFont = 0L;
00523 }
00524 
00525 void KGlobalSettings::rereadPathSettings()
00526 {
00527     kdDebug() << "KGlobalSettings::rereadPathSettings" << endl;
00528     delete s_autostartPath;
00529     s_autostartPath = 0L;
00530     delete s_trashPath;
00531     s_trashPath = 0L;
00532     delete s_desktopPath;
00533     s_desktopPath = 0L;
00534     delete s_documentPath;
00535     s_documentPath = 0L;
00536 }
00537 
00538 KGlobalSettings::KMouseSettings & KGlobalSettings::mouseSettings()
00539 {
00540     if ( ! s_mouseSettings )
00541     {
00542         s_mouseSettings = new KMouseSettings;
00543         KMouseSettings & s = *s_mouseSettings; // for convenience
00544 
00545         KConfigGroupSaver cgs( KGlobal::config(), "Mouse" );
00546         QString setting = KGlobal::config()->readEntry("MouseButtonMapping");
00547         if (setting == "RightHanded")
00548             s.handed = KMouseSettings::RightHanded;
00549         else if (setting == "LeftHanded")
00550             s.handed = KMouseSettings::LeftHanded;
00551         else
00552         {
00553 #ifdef Q_WS_X11
00554             // get settings from X server
00555             // This is a simplified version of the code in input/mouse.cpp
00556             // Keep in sync !
00557             s.handed = KMouseSettings::RightHanded;
00558             unsigned char map[5];
00559             switch (XGetPointerMapping(kapp->getDisplay(), map, 5))
00560             {
00561                 case 2:
00562                     if ( (int)map[0] == 1 && (int)map[1] == 2 )
00563                         s.handed = KMouseSettings::RightHanded;
00564                     else if ( (int)map[0] == 2 && (int)map[1] == 1 )
00565                         s.handed = KMouseSettings::LeftHanded;
00566                     break;
00567                 case 3:
00568                 case 5:
00569                     if ( (int)map[0] == 1 && (int)map[2] == 3 )
00570                         s.handed = KMouseSettings::RightHanded;
00571                     else if ( (int)map[0] == 3 && (int)map[2] == 1 )
00572                         s.handed = KMouseSettings::LeftHanded;
00573                     break;
00574                 default:
00575                     break;
00576             }
00577 #else
00578         // FIXME(E): Implement in Qt Embedded
00579 #endif
00580         }
00581     }
00582     return *s_mouseSettings;
00583 }
00584 
00585 void KGlobalSettings::rereadMouseSettings()
00586 {
00587     delete s_mouseSettings;
00588     s_mouseSettings = 0L;
00589 }
00590 
00591 bool KGlobalSettings::isMultiHead()
00592 {
00593     QCString multiHead = getenv("KDE_MULTIHEAD");
00594     if (!multiHead.isEmpty()) {
00595         return (multiHead.lower() == "true");
00596     }
00597     return false;
00598 }
00599 
00600 bool KGlobalSettings::wheelMouseZooms()
00601 {
00602     KConfigGroup group( KGlobal::config(), "KDE" );
00603     return group.readBoolEntry( "WheelMouseZooms", KDE_DEFAULT_WHEEL_ZOOM );
00604 }
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.4.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Sun Feb 27 22:14:46 2005 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001