00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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
00029 #include <kdebug.h>
00030 #include <kcursor.h>
00031 #include <kapplication.h>
00032 #include <kpixmap.h>
00033 #include <kiconeffect.h>
00034 #include <kpixmapeffect.h>
00035
00036 using namespace KexiUtils;
00037
00038 DelayedCursorHandler::DelayedCursorHandler()
00039 : startedOrActive(false)
00040 {
00041 connect(&timer, SIGNAL(timeout()), this, SLOT(show()));
00042 }
00043 void DelayedCursorHandler::start(bool noDelay) {
00044 startedOrActive = true;
00045 timer.start(noDelay ? 0 : 1000, true);
00046 }
00047 void DelayedCursorHandler::stop() {
00048 startedOrActive = false;
00049 timer.stop();
00050 QApplication::restoreOverrideCursor();
00051 }
00052 void DelayedCursorHandler::show() {
00053 QApplication::setOverrideCursor( KCursor::waitCursor() );
00054 }
00055
00056 DelayedCursorHandler _delayedCursorHandler;
00057
00058 void KexiUtils::setWaitCursor(bool noDelay) {
00059 if (kapp->guiEnabled())
00060 _delayedCursorHandler.start(noDelay);
00061 }
00062 void KexiUtils::removeWaitCursor() {
00063 if (kapp->guiEnabled())
00064 _delayedCursorHandler.stop();
00065 }
00066
00067 WaitCursor::WaitCursor(bool noDelay)
00068 {
00069 setWaitCursor(noDelay);
00070 }
00071
00072 WaitCursor::~WaitCursor()
00073 {
00074 removeWaitCursor();
00075 }
00076
00077 WaitCursorRemover::WaitCursorRemover()
00078 {
00079 m_reactivateCursor = _delayedCursorHandler.startedOrActive;
00080 _delayedCursorHandler.stop();
00081 }
00082
00083 WaitCursorRemover::~WaitCursorRemover()
00084 {
00085 _delayedCursorHandler.start(true);
00086 }
00087
00088
00089
00090 QString KexiUtils::fileDialogFilterString(const KMimeType::Ptr& mime, bool kdeFormat)
00091 {
00092 if (mime==0)
00093 return QString::null;
00094
00095 QString str;
00096 if (kdeFormat) {
00097 if (mime->patterns().isEmpty())
00098 str = "*";
00099 else
00100 str = mime->patterns().join(" ");
00101 str += "|";
00102 }
00103 str += mime->comment();
00104 if (!mime->patterns().isEmpty() || !kdeFormat) {
00105 str += " (";
00106 if (mime->patterns().isEmpty())
00107 str += "*";
00108 else
00109 str += mime->patterns().join("; ");
00110 str += ")";
00111 }
00112 if (kdeFormat)
00113 str += "\n";
00114 else
00115 str += ";;";
00116 return str;
00117 }
00118
00119 QString KexiUtils::fileDialogFilterString(const QString& mimeString, bool kdeFormat)
00120 {
00121 KMimeType::Ptr ptr = KMimeType::mimeType(mimeString);
00122 return fileDialogFilterString( ptr, kdeFormat );
00123 }
00124
00125 QString KexiUtils::fileDialogFilterStrings(const QStringList& mimeStrings, bool kdeFormat)
00126 {
00127 QString ret;
00128 QStringList::ConstIterator endIt = mimeStrings.constEnd();
00129 for(QStringList::ConstIterator it = mimeStrings.constBegin(); it != endIt; ++it)
00130 ret += fileDialogFilterString(*it, kdeFormat);
00131 return ret;
00132 }
00133
00134 QColor KexiUtils::blendedColors(const QColor& c1, const QColor& c2, int factor1, int factor2)
00135 {
00136 return QColor(
00137 int( (c1.red()*factor1+c2.red()*factor2)/(factor1+factor2) ),
00138 int( (c1.green()*factor1+c2.green()*factor2)/(factor1+factor2) ),
00139 int( (c1.blue()*factor1+c2.blue()*factor2)/(factor1+factor2) ) );
00140 }
00141
00142 QColor KexiUtils::contrastColor(const QColor& c)
00143 {
00144 int g = qGray( c.rgb() );
00145 if (g>110)
00146 return c.dark(200);
00147 else if (g>80)
00148 return c.light(150);
00149 else if (g>20)
00150 return c.light(300);
00151 return Qt::gray;
00152 }
00153
00154 QColor KexiUtils::bleachedColor(const QColor& c, int factor)
00155 {
00156 int h, s, v;
00157 c.getHsv( &h, &s, &v );
00158 QColor c2;
00159 if (factor < 100)
00160 factor = 100;
00161 if (s>=250 && v>=250)
00162 s = QMAX(0, s - factor - 50);
00163 else if (s<=5 && s<=5)
00164 v += factor-50;
00165 c2.setHsv(h, s, QMIN(255,v + factor-100));
00166 return c2;
00167 }
00168
00169 QIconSet KexiUtils::colorizeIconToTextColor(const QPixmap& icon, const QPalette& palette)
00170 {
00171 QPixmap pm(
00172 KIconEffect().apply( icon, KIconEffect::Colorize, 1.0f, palette.active().buttonText(), false ) );
00173
00174 KPixmap kpm(pm);
00175 return QIconSet(
00176 KPixmapEffect::fade( kpm, 0.33, palette.active().button() ) );
00177 }
00178
00179 void KexiUtils::serializeMap(const QMap<QString,QString>& map, const QByteArray& array)
00180 {
00181 QDataStream ds(array, IO_WriteOnly);
00182 ds << map;
00183 }
00184
00185 void KexiUtils::serializeMap(const QMap<QString,QString>& map, QString& string)
00186 {
00187 QByteArray array;
00188 QDataStream ds(array, IO_WriteOnly);
00189 ds << map;
00190 kdDebug() << array[3] << " " << array[4] << " " << array[5] << endl;
00191 const uint size = array.size();
00192 string = QString::null;
00193 string.reserve(size);
00194 for (uint i=0; i<size; i++) {
00195 string[i]=QChar(ushort(array[i]+1));
00196 }
00197 }
00198
00199 QMap<QString,QString> KexiUtils::deserializeMap(const QByteArray& array)
00200 {
00201 QMap<QString,QString> map;
00202 QDataStream ds(array, IO_ReadOnly);
00203 ds >> map;
00204 return map;
00205 }
00206
00207 QMap<QString,QString> KexiUtils::deserializeMap(const QString& string)
00208 {
00209 const uint size = string.length();
00210 QCString cstr(string.latin1());
00211 QByteArray array( size );
00212 for (uint i=0; i<size; i++) {
00213 array[i] = char(string[i].unicode()-1);
00214 }
00215 QMap<QString,QString> map;
00216 QDataStream ds(array, IO_ReadOnly);
00217 ds >> map;
00218 return map;
00219 }
00220
00221 QString KexiUtils::stringToFileName(const QString& string)
00222 {
00223 QString _string(string);
00224 _string.replace(QRegExp("[\\\\/:\\*?\"<>|]"), " ");
00225 return _string.simplifyWhiteSpace();
00226 }
00227
00228 void KexiUtils::simpleCrypt(QString& string)
00229 {
00230 for (uint i=0; i<string.length(); i++)
00231 string[i] = QChar( string[i].unicode() + 47 + i );
00232 }
00233
00234 void KexiUtils::simpleDecrypt(QString& string)
00235 {
00236 for (uint i=0; i<string.length(); i++)
00237 string[i] = QChar( string[i].unicode() - 47 - i );
00238 }
00239
00240 void KexiUtils::drawPixmap( QPainter& p, int lineWidth, const QRect& rect,
00241 const QPixmap& pixmap, int alignment, bool scaledContents, bool keepAspectRatio)
00242 {
00243 if (pixmap.isNull())
00244 return;
00245
00246 const bool fast = pixmap.width()>1000 && pixmap.height()>800;
00247 const int w = rect.width()-lineWidth-lineWidth;
00248 const int h = rect.height()-lineWidth-lineWidth;
00251 QPixmap pixmapBuffer;
00252 QPainter p2;
00253 QPainter *target;
00254 if (fast) {
00255 target = &p;
00256 }
00257 else {
00258
00259
00260 target = &p2;
00261 }
00263
00264
00265 QPoint pos;
00266 if (scaledContents) {
00267 if (keepAspectRatio) {
00268 QImage img(pixmap.convertToImage());
00269 img = img.smoothScale(w, h, QImage::ScaleMin);
00270 pos = rect.topLeft();
00271 if (img.width() < w) {
00272 int hAlign = QApplication::horizontalAlignment( alignment );
00273 if ( hAlign & Qt::AlignRight )
00274 pos.setX(pos.x() + w-img.width());
00275 else if ( hAlign & Qt::AlignHCenter )
00276 pos.setX(pos.x() + w/2-img.width()/2);
00277 }
00278 else if (img.height() < h) {
00279 if ( alignment & Qt::AlignBottom )
00280 pos.setY(pos.y() + h-img.height());
00281 else if ( alignment & Qt::AlignVCenter )
00282 pos.setY(pos.y() + h/2-img.height()/2);
00283 }
00284 pixmapBuffer.convertFromImage(img);
00285 if (!fast) {
00286 p2.begin(&pixmapBuffer, p.device());
00287 }
00288 else
00289 target->drawPixmap(pos, pixmapBuffer);
00290 }
00291 else {
00292 if (!fast) {
00293 pixmapBuffer.resize(rect.size()-QSize(lineWidth, lineWidth));
00294 p2.begin(&pixmapBuffer, p.device());
00295 p2.drawPixmap(QRect(rect.x(), rect.y(), w, h), pixmap);
00296 }
00297 else
00298 target->drawPixmap(QRect(rect.x() + lineWidth, rect.y() + lineWidth, w, h), pixmap);
00299 }
00300 }
00301 else {
00302 int hAlign = QApplication::horizontalAlignment( alignment );
00303 if ( hAlign & Qt::AlignRight )
00304 pos.setX(pos.x() + w-pixmap.width());
00305 else if ( hAlign & Qt::AlignHCenter )
00306 pos.setX(pos.x() + w/2-pixmap.width()/2);
00307 else
00308 pos.setX(pos.x());
00309
00310 if ( alignment & Qt::AlignBottom )
00311 pos.setY(pos.y() + h-pixmap.height());
00312 else if ( alignment & Qt::AlignVCenter )
00313 pos.setY(pos.y() + h/2-pixmap.height()/2);
00314 else
00315 pos.setY(pos.y());
00316
00317
00318
00319 p.drawPixmap(lineWidth, lineWidth, pixmap);
00320 }
00321 if (scaledContents && !fast && p.isActive()) {
00322 p2.end();
00323 bitBlt( p.device(),
00324
00325
00326 (int)p.worldMatrix().dx() + rect.x() + lineWidth + pos.x(),
00327 (int)p.worldMatrix().dy() + rect.y() + lineWidth + pos.y(),
00328 &pixmapBuffer);
00329 }
00330 }
00331
00332 QString KexiUtils::ptrToStringInternal(void* ptr, uint size)
00333 {
00334 QString str;
00335 unsigned char* cstr_ptr = (unsigned char*)&ptr;
00336 for (uint i=0; i<size; i++) {
00337 QString s;
00338 s.sprintf("%2.2x", cstr_ptr[i]);
00339 str.append( s );
00340 }
00341 return str;
00342 }
00343
00344 void* KexiUtils::stringToPtrInternal(const QString& str, uint size)
00345 {
00346 QByteArray array(size);
00347 if ((str.length()/2)<size)
00348 return 0;
00349 bool ok;
00350 for (uint i=0; i<size; i++) {
00351 array[i]=(unsigned char)(str.mid(i*2, 2).toUInt(&ok, 16));
00352 if (!ok)
00353 return 0;
00354 }
00355 return *(void**)(array.data());
00356 }
00357
00358 #include "utils_p.moc"