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
00025 #include <kdebug.h>
00026 #include <kcursor.h>
00027 #include <kapplication.h>
00028
00029 using namespace KexiUtils;
00030
00031 DelayedCursorHandler::DelayedCursorHandler() {
00032 connect(&timer, SIGNAL(timeout()), this, SLOT(show()));
00033 }
00034 void DelayedCursorHandler::start(bool noDelay) {
00035 timer.start(noDelay ? 0 : 1000, true);
00036 }
00037 void DelayedCursorHandler::stop() {
00038 timer.stop();
00039 QApplication::restoreOverrideCursor();
00040 }
00041 void DelayedCursorHandler::show() {
00042 QApplication::setOverrideCursor( KCursor::waitCursor() );
00043 }
00044
00045 DelayedCursorHandler _delayedCursorHandler;
00046
00047 void KexiUtils::setWaitCursor(bool noDelay) {
00048 if (kapp->guiEnabled())
00049 _delayedCursorHandler.start(noDelay);
00050 }
00051 void KexiUtils::removeWaitCursor() {
00052 if (kapp->guiEnabled())
00053 _delayedCursorHandler.stop();
00054 }
00055
00056 WaitCursor::WaitCursor(bool noDelay)
00057 {
00058 setWaitCursor(noDelay);
00059 }
00060
00061 WaitCursor::~WaitCursor()
00062 {
00063 removeWaitCursor();
00064 }
00065
00066
00067
00068 QString KexiUtils::fileDialogFilterString(const KMimeType::Ptr& mime, bool kdeFormat)
00069 {
00070 if (mime==0)
00071 return QString::null;
00072
00073 QString str;
00074 if (kdeFormat) {
00075 if (mime->patterns().isEmpty())
00076 str = "*";
00077 else
00078 str = mime->patterns().join(" ");
00079 str += "|";
00080 }
00081 str += mime->comment();
00082 if (!mime->patterns().isEmpty() || !kdeFormat) {
00083 str += " (";
00084 if (mime->patterns().isEmpty())
00085 str += "*";
00086 else
00087 str += mime->patterns().join("; ");
00088 str += ")";
00089 }
00090 if (kdeFormat)
00091 str += "\n";
00092 else
00093 str += ";;";
00094 return str;
00095 }
00096
00097 QString KexiUtils::fileDialogFilterString(const QString& mimeString, bool kdeFormat)
00098 {
00099 KMimeType::Ptr ptr = KMimeType::mimeType(mimeString);
00100 return fileDialogFilterString( ptr, kdeFormat );
00101 }
00102
00103 QString KexiUtils::fileDialogFilterStrings(const QStringList& mimeStrings, bool kdeFormat)
00104 {
00105 QString ret;
00106 QStringList::ConstIterator endIt = mimeStrings.constEnd();
00107 for(QStringList::ConstIterator it = mimeStrings.constBegin(); it != endIt; ++it)
00108 ret += fileDialogFilterString(*it, kdeFormat);
00109 return ret;
00110 }
00111
00112 QColor KexiUtils::blendedColors(const QColor& c1, const QColor& c2, int factor1, int factor2)
00113 {
00114 return QColor(
00115 int( (c1.red()*factor1+c2.red()*factor2)/(factor1+factor2) ),
00116 int( (c1.green()*factor1+c2.green()*factor2)/(factor1+factor2) ),
00117 int( (c1.blue()*factor1+c2.blue()*factor2)/(factor1+factor2) ) );
00118 }
00119
00120 QColor KexiUtils::contrastColor(const QColor& c)
00121 {
00122 int g = qGray( c.rgb() );
00123 if (g>110)
00124 return c.dark(200);
00125 else if (g>80)
00126 return c.light(150);
00127 else if (g>20)
00128 return c.light(300);
00129 return Qt::gray;
00130 }
00131
00132 QColor KexiUtils::bleachedColor(const QColor& c, int factor)
00133 {
00134 int h, s, v;
00135 c.getHsv( &h, &s, &v );
00136 QColor c2;
00137 if (factor < 100)
00138 factor = 100;
00139 if (s>=250 && v>=250)
00140 s = QMAX(0, s - factor - 50);
00141 else if (s<=5 && s<=5)
00142 v += factor-50;
00143 c2.setHsv(h, s, QMIN(255,v + factor-100));
00144 return c2;
00145 }
00146
00147 void KexiUtils::serializeMap(const QMap<QString,QString>& map, QByteArray& array)
00148 {
00149 QDataStream ds(array, IO_WriteOnly);
00150 ds << map;
00151 }
00152
00153 void KexiUtils::serializeMap(const QMap<QString,QString>& map, QString& string)
00154 {
00155 QByteArray array;
00156 QDataStream ds(array, IO_WriteOnly);
00157 ds << map;
00158 kdDebug() << array[3] << " " << array[4] << " " << array[5] << endl;
00159 const uint size = array.size();
00160 string = QString::null;
00161 string.reserve(size);
00162 for (uint i=0; i<size; i++) {
00163 string[i]=QChar(ushort(array[i]+1));
00164 }
00165 }
00166
00167 QMap<QString,QString> KexiUtils::deserializeMap(const QByteArray& array)
00168 {
00169 QMap<QString,QString> map;
00170 QDataStream ds(array, IO_ReadOnly);
00171 ds >> map;
00172 return map;
00173 }
00174
00175 QMap<QString,QString> KexiUtils::deserializeMap(const QString& string)
00176 {
00177 const uint size = string.length();
00178 QCString cstr(string.latin1());
00179 QByteArray array( size );
00180 for (uint i=0; i<size; i++) {
00181 array[i] = char(string[i].unicode()-1);
00182 }
00183 QMap<QString,QString> map;
00184 QDataStream ds(array, IO_ReadOnly);
00185 ds >> map;
00186 return map;
00187 }
00188
00189 QString KexiUtils::stringToFileName(const QString& string)
00190 {
00191 QString _string(string);
00192 _string.replace(QRegExp("[\\\\/:\\*?\"<>|]"), " ");
00193 return _string.simplifyWhiteSpace();
00194 }
00195
00196 void KexiUtils::simpleCrypt(QString& string)
00197 {
00198 for (uint i=0; i<string.length(); i++)
00199 string[i] = QChar( string[i].unicode() + 47 + i );
00200 }
00201
00202 void KexiUtils::simpleDecrypt(QString& string)
00203 {
00204 for (uint i=0; i<string.length(); i++)
00205 string[i] = QChar( string[i].unicode() - 47 - i );
00206 }
00207
00208 #include "utils_p.moc"