kexi

utils.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2003-2006 Jaroslaw Staniek <js@iidea.pl>
00003 
00004    This program is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This program is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this program; see the file COPYING.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include "utils.h"
00021 #include "utils_p.h"
00022 
00023 #include <qregexp.h>
00024 #include <qpainter.h>
00025 #include <qimage.h>
00026 #include <qwmatrix.h>
00027 #include <qiconset.h>
00028 #include <qbitmap.h>
00029 
00030 #include <kdebug.h>
00031 #include <kcursor.h>
00032 #include <kapplication.h>
00033 #include <kpixmap.h>
00034 #include <kiconeffect.h>
00035 #include <kpixmapeffect.h>
00036 #include <kiconloader.h>
00037 
00038 using namespace KexiUtils;
00039 
00040 DelayedCursorHandler::DelayedCursorHandler() 
00041  : startedOrActive(false)
00042 {
00043     connect(&timer, SIGNAL(timeout()), this, SLOT(show()));
00044 }
00045 void DelayedCursorHandler::start(bool noDelay) {
00046     startedOrActive = true;
00047     timer.start(noDelay ? 0 : 1000, true);
00048 }
00049 void DelayedCursorHandler::stop() {
00050     startedOrActive = false;
00051     timer.stop();
00052     QApplication::restoreOverrideCursor();
00053 }
00054 void DelayedCursorHandler::show() {
00055     QApplication::setOverrideCursor( KCursor::waitCursor() );
00056 }
00057 
00058 DelayedCursorHandler _delayedCursorHandler;
00059 
00060 void KexiUtils::setWaitCursor(bool noDelay) {
00061     if (kapp->guiEnabled())
00062         _delayedCursorHandler.start(noDelay);
00063 }
00064 void KexiUtils::removeWaitCursor() {
00065     if (kapp->guiEnabled())
00066         _delayedCursorHandler.stop();
00067 }
00068 
00069 WaitCursor::WaitCursor(bool noDelay)
00070 {
00071     setWaitCursor(noDelay);
00072 }
00073 
00074 WaitCursor::~WaitCursor()
00075 {
00076     removeWaitCursor();
00077 }
00078 
00079 WaitCursorRemover::WaitCursorRemover()
00080 {
00081     m_reactivateCursor = _delayedCursorHandler.startedOrActive;
00082     _delayedCursorHandler.stop();
00083 }
00084 
00085 WaitCursorRemover::~WaitCursorRemover()
00086 {
00087     _delayedCursorHandler.start(true);
00088 }
00089 
00090 //--------------------------------------------------------------------------------
00091 
00092 QString KexiUtils::fileDialogFilterString(const KMimeType::Ptr& mime, bool kdeFormat)
00093 {
00094     if (mime==0)
00095         return QString::null;
00096 
00097     QString str;
00098     if (kdeFormat) {
00099         if (mime->patterns().isEmpty())
00100             str = "*";
00101         else
00102             str = mime->patterns().join(" ");
00103         str += "|";
00104     }
00105     str += mime->comment();
00106     if (!mime->patterns().isEmpty() || !kdeFormat) {
00107         str += " (";
00108         if (mime->patterns().isEmpty())
00109             str += "*";
00110         else
00111             str += mime->patterns().join("; ");
00112         str += ")";
00113     }
00114     if (kdeFormat)
00115         str += "\n";
00116     else
00117         str += ";;";
00118     return str;
00119 }
00120 
00121 QString KexiUtils::fileDialogFilterString(const QString& mimeString, bool kdeFormat)
00122 {
00123     KMimeType::Ptr ptr = KMimeType::mimeType(mimeString);
00124     return fileDialogFilterString( ptr, kdeFormat );
00125 }
00126 
00127 QString KexiUtils::fileDialogFilterStrings(const QStringList& mimeStrings, bool kdeFormat)
00128 {
00129     QString ret;
00130     QStringList::ConstIterator endIt = mimeStrings.constEnd();
00131     for(QStringList::ConstIterator it = mimeStrings.constBegin(); it != endIt; ++it)
00132         ret += fileDialogFilterString(*it, kdeFormat);
00133     return ret;
00134 }
00135 
00136 QColor KexiUtils::blendedColors(const QColor& c1, const QColor& c2, int factor1, int factor2)
00137 {
00138     return QColor(
00139         int( (c1.red()*factor1+c2.red()*factor2)/(factor1+factor2) ),
00140         int( (c1.green()*factor1+c2.green()*factor2)/(factor1+factor2) ),
00141         int( (c1.blue()*factor1+c2.blue()*factor2)/(factor1+factor2) ) );
00142 }
00143 
00144 QColor KexiUtils::contrastColor(const QColor& c)
00145 {
00146     int g = qGray( c.rgb() );
00147     if (g>110)
00148         return c.dark(200);
00149     else if (g>80)
00150         return c.light(150);
00151     else if (g>20)
00152         return c.light(300);
00153     return Qt::gray;
00154 }
00155 
00156 QColor KexiUtils::bleachedColor(const QColor& c, int factor)
00157 {
00158     int h, s, v;
00159     c.getHsv( &h, &s, &v );
00160     QColor c2;
00161     if (factor < 100)
00162         factor = 100;
00163     if (s>=250 && v>=250) //for colors like cyan or red, make the result more white
00164         s = QMAX(0, s - factor - 50);
00165     else if (s<=5 && s<=5)
00166         v += factor-50;
00167     c2.setHsv(h, s, QMIN(255,v + factor-100));
00168     return c2;
00169 }
00170 
00171 QIconSet KexiUtils::colorizeIconToTextColor(const QPixmap& icon, const QPalette& palette)
00172 {
00173     QPixmap pm(
00174         KIconEffect().apply( icon, KIconEffect::Colorize, 1.0f, palette.active().buttonText(), false ) );
00175 
00176     KPixmap kpm(pm);
00177     return QIconSet(
00178         KPixmapEffect::fade( kpm, 0.33, palette.active().button() ) );
00179 }
00180 
00181 QPixmap KexiUtils::emptyIcon(KIcon::Group iconGroup)
00182 {
00183     QPixmap noIcon( IconSize( iconGroup ), IconSize( iconGroup ) );
00184     QBitmap bmpNoIcon(noIcon.size());
00185     bmpNoIcon.fill(Qt::color0);
00186     noIcon.setMask(bmpNoIcon);
00187     return noIcon;
00188 }
00189 
00190 void KexiUtils::serializeMap(const QMap<QString,QString>& map, const QByteArray& array)
00191 {
00192     QDataStream ds(array, IO_WriteOnly);
00193     ds << map;
00194 }
00195 
00196 void KexiUtils::serializeMap(const QMap<QString,QString>& map, QString& string)
00197 {
00198     QByteArray array;
00199     QDataStream ds(array, IO_WriteOnly);
00200     ds << map;
00201     kdDebug() << array[3] << " " << array[4] << " " << array[5] << endl;
00202     const uint size = array.size();
00203     string = QString::null;
00204     string.reserve(size);
00205     for (uint i=0; i<size; i++) {
00206         string[i]=QChar(ushort(array[i]+1));
00207     }
00208 }
00209 
00210 QMap<QString,QString> KexiUtils::deserializeMap(const QByteArray& array)
00211 {
00212     QMap<QString,QString> map;
00213     QDataStream ds(array, IO_ReadOnly);
00214     ds >> map;
00215     return map;
00216 }
00217 
00218 QMap<QString,QString> KexiUtils::deserializeMap(const QString& string)
00219 {
00220     const uint size = string.length();
00221     QCString cstr(string.latin1());
00222     QByteArray array( size );
00223     for (uint i=0; i<size; i++) {
00224         array[i] = char(string[i].unicode()-1);
00225     }
00226     QMap<QString,QString> map;
00227     QDataStream ds(array, IO_ReadOnly);
00228     ds >> map;
00229     return map;
00230 }
00231 
00232 QString KexiUtils::stringToFileName(const QString& string)
00233 {
00234     QString _string(string);
00235     _string.replace(QRegExp("[\\\\/:\\*?\"<>|]"), " ");
00236     return _string.simplifyWhiteSpace();
00237 }
00238 
00239 void KexiUtils::simpleCrypt(QString& string)
00240 {
00241     for (uint i=0; i<string.length(); i++)
00242         string[i] = QChar( string[i].unicode() + 47 + i );
00243 }
00244 
00245 void KexiUtils::simpleDecrypt(QString& string)
00246 {
00247     for (uint i=0; i<string.length(); i++)
00248         string[i] = QChar( string[i].unicode() - 47 - i );
00249 }
00250 
00251 void KexiUtils::drawPixmap( QPainter& p, int lineWidth, const QRect& rect, 
00252     const QPixmap& pixmap, int alignment, bool scaledContents, bool keepAspectRatio)
00253 {
00254     if (pixmap.isNull())
00255         return;
00256 
00257     const bool fast = pixmap.width()>1000 && pixmap.height()>800; //fast drawing needed
00258     const int w = rect.width()-lineWidth-lineWidth;
00259     const int h = rect.height()-lineWidth-lineWidth;
00262     QPixmap pixmapBuffer;
00263     QPainter p2;
00264     QPainter *target;
00265     if (fast) {
00266         target = &p;
00267     }
00268     else {
00269 //moved     pixmapBuffer.resize(rect.size()-QSize(lineWidth, lineWidth));
00270 //moved     p2.begin(&pm, p.device());
00271         target = &p2;
00272     }
00274 //  target->fillRect(0,0,rect.width(),rect.height(), backgroundColor);
00275 
00276     QPoint pos;
00277     if (scaledContents) {
00278         if (keepAspectRatio) {
00279             QImage img(pixmap.convertToImage());
00280             img = img.smoothScale(w, h, QImage::ScaleMin);
00281             pos = rect.topLeft(); //0, 0);
00282             if (img.width() < w) {
00283                 int hAlign = QApplication::horizontalAlignment( alignment );
00284                 if ( hAlign & Qt::AlignRight )
00285                     pos.setX(pos.x() + w-img.width());
00286                 else if ( hAlign & Qt::AlignHCenter )
00287                     pos.setX(pos.x() + w/2-img.width()/2);
00288             }
00289             else if (img.height() < h) {
00290                 if ( alignment & Qt::AlignBottom )
00291                     pos.setY(pos.y() + h-img.height());
00292                 else if ( alignment & Qt::AlignVCenter )
00293                     pos.setY(pos.y() + h/2-img.height()/2);
00294             }
00295             pixmapBuffer.convertFromImage(img);
00296             if (!fast) {
00297                 p2.begin(&pixmapBuffer, p.device());
00298             }
00299             else
00300                 target->drawPixmap(pos, pixmapBuffer);
00301         }
00302         else {
00303             if (!fast) {
00304                 pixmapBuffer.resize(rect.size()-QSize(lineWidth, lineWidth));
00305                 p2.begin(&pixmapBuffer, p.device());
00306                 p2.drawPixmap(QRect(rect.x(), rect.y(), w, h), pixmap);
00307             }
00308             else
00309                 target->drawPixmap(QRect(rect.x() + lineWidth, rect.y() + lineWidth, w, h), pixmap);
00310         }
00311     }
00312     else {
00313         int hAlign = QApplication::horizontalAlignment( alignment );
00314         if ( hAlign & Qt::AlignRight )
00315             pos.setX(pos.x() + w-pixmap.width());
00316         else if ( hAlign & Qt::AlignHCenter )
00317             pos.setX(pos.x() + w/2-pixmap.width()/2);
00318         else //left, etc.
00319             pos.setX(pos.x());
00320 
00321         if ( alignment & Qt::AlignBottom )
00322             pos.setY(pos.y() + h-pixmap.height());
00323         else if ( alignment & Qt::AlignVCenter )
00324             pos.setY(pos.y() + h/2-pixmap.height()/2);
00325         else //top, etc. 
00326             pos.setY(pos.y());
00327 //      target->drawPixmap(pos, pixmap);
00328 //      if (!fast)
00329 //          p2.begin(&pixmapBuffer, p.device());
00330         p.drawPixmap(lineWidth+pos.x(), lineWidth+pos.y(), pixmap);
00331     }
00332     if (scaledContents && !fast && p.isActive()) {
00333         p2.end();
00334         bitBlt( p.device(), 
00335 //          pos.x(), 
00336 //          pos.y(), 
00337             (int)p.worldMatrix().dx() + rect.x() + lineWidth + pos.x(), 
00338             (int)p.worldMatrix().dy() + rect.y() + lineWidth + pos.y(), 
00339             &pixmapBuffer);
00340     }
00341 }
00342 
00343 QString KexiUtils::ptrToStringInternal(void* ptr, uint size)
00344 {
00345     QString str;
00346     unsigned char* cstr_ptr = (unsigned char*)&ptr;
00347     for (uint i=0; i<size; i++) {
00348         QString s;
00349         s.sprintf("%2.2x", cstr_ptr[i]);
00350         str.append( s );
00351     }
00352     return str;
00353 }
00354 
00355 void* KexiUtils::stringToPtrInternal(const QString& str, uint size)
00356 {
00357     QByteArray array(size);
00358     if ((str.length()/2)<size)
00359         return 0;
00360     bool ok;
00361     for (uint i=0; i<size; i++) {
00362         array[i]=(unsigned char)(str.mid(i*2, 2).toUInt(&ok, 16));
00363         if (!ok)
00364             return 0;
00365     }
00366     return *(void**)(array.data());
00367 }
00368 
00369 #include "utils_p.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys