tools/designer/src/components/formeditor/brushmanagerproxy.cpp

Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** Copyright (C) 1992-2006 Trolltech ASA. All rights reserved.
00004 **
00005 ** This file is part of the Qt Designer of the Qt Toolkit.
00006 **
00007 ** This file may be used under the terms of the GNU General Public
00008 ** License version 2.0 as published by the Free Software Foundation
00009 ** and appearing in the file LICENSE.GPL included in the packaging of
00010 ** this file.  Please review the following information to ensure GNU
00011 ** General Public Licensing requirements will be met:
00012 ** http://www.trolltech.com/products/qt/opensource.html
00013 **
00014 ** If you are unsure which license is appropriate for your use, please
00015 ** review the following information:
00016 ** http://www.trolltech.com/products/qt/licensing.html or contact the
00017 ** sales department at sales@trolltech.com.
00018 **
00019 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00020 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00021 **
00022 ****************************************************************************/
00023 
00024 #include "qtbrushmanager.h"
00025 #include "brushmanagerproxy.h"
00026 #include "qsimpleresource_p.h"
00027 #include "ui4_p.h"
00028 
00029 #include <QDomDocument>
00030 
00031 namespace qdesigner_internal {
00032 
00033 class BrushManagerProxyPrivate
00034 {
00035     BrushManagerProxy *q_ptr;
00036     Q_DECLARE_PUBLIC(BrushManagerProxy)
00037 public:
00038     void brushAdded(const QString &name, const QBrush &brush);
00039     void brushRemoved(const QString &name);
00040     QString uniqueBrushFileName(const QString &brushName) const;
00041 
00042     QtBrushManager *theManager;
00043     QString theBrushFolder;
00044     QDesignerFormEditorInterface *theCore;
00045     QMap<QString, QString> theFileToBrush;
00046     QMap<QString, QString> theBrushToFile;
00047 };
00048 
00049 }  // namespace qdesigner_internal
00050 
00051 using namespace qdesigner_internal;
00052 
00053 void BrushManagerProxyPrivate::brushAdded(const QString &name, const QBrush &brush)
00054 {
00055     QString filename = uniqueBrushFileName(name);
00056 
00057     QDir designerDir(QDir::homePath() + QDir::separator() + QLatin1String(".designer"));
00058     if (!designerDir.exists(QLatin1String("brushes")))
00059         designerDir.mkdir(QLatin1String("brushes"));
00060 
00061     QFile file(theBrushFolder + QDir::separator() +filename);
00062     if (!file.open(QIODevice::WriteOnly))
00063         return;
00064 
00065     QSimpleResource resource(theCore);
00066 
00067     DomBrush *dom = resource.saveBrush(brush);
00068     QDomDocument doc;
00069     QDomElement elem = doc.createElement(QLatin1String("description"));
00070     elem.setAttribute(QLatin1String("name"), name);
00071     elem.appendChild(dom->write(doc));
00072     doc.appendChild(elem);
00073     file.write(doc.toString().toUtf8());
00074 
00075     file.close();
00076 
00077     theFileToBrush[filename] = name;
00078     theBrushToFile[name] = filename;
00079 
00080     delete dom;
00081 }
00082 
00083 void BrushManagerProxyPrivate::brushRemoved(const QString &name)
00084 {
00085     QDir brushDir(theBrushFolder);
00086 
00087     QString filename = theBrushToFile[name];
00088     brushDir.remove(filename);
00089     theBrushToFile.remove(name);
00090     theFileToBrush.remove(filename);
00091 }
00092 
00093 QString BrushManagerProxyPrivate::uniqueBrushFileName(const QString &brushName) const
00094 {
00095     QString filename = brushName.toLower() + QLatin1String(".br");
00096     int i = 0;
00097     while (theFileToBrush.contains(filename))
00098         filename = brushName.toLower() + QString::number(++i) + QLatin1String(".br");
00099     return filename;
00100 }
00101 
00102 
00103 BrushManagerProxy::BrushManagerProxy(QDesignerFormEditorInterface *core, QObject *parent)
00104     : QObject(parent)
00105 {
00106     d_ptr = new BrushManagerProxyPrivate;
00107     d_ptr->q_ptr = this;
00108 
00109     d_ptr->theManager = 0;
00110     d_ptr->theBrushFolder = QDir::homePath()
00111                     + QDir::separator()
00112                     + QLatin1String(".designer")
00113                     + QDir::separator()
00114                     + QLatin1String("brushes");
00115     d_ptr->theCore = core;
00116 }
00117 
00118 BrushManagerProxy::~BrushManagerProxy()
00119 {
00120     delete d_ptr;
00121 }
00122 
00123 void BrushManagerProxy::setBrushManager(QtBrushManager *manager)
00124 {
00125     if (d_ptr->theManager == manager)
00126         return;
00127 
00128     if (d_ptr->theManager) {
00129         disconnect(d_ptr->theManager, SIGNAL(brushAdded(const QString &, const QBrush &)),
00130                     this, SLOT(brushAdded(const QString &, const QBrush &)));
00131         disconnect(d_ptr->theManager, SIGNAL(brushRemoved(const QString &)),
00132                     this, SLOT(brushRemoved(const QString &)));
00133     }
00134 
00135     d_ptr->theManager = manager;
00136 
00137     if (!d_ptr->theManager)
00138         return;
00139 
00140     // clear the manager
00141     QMap<QString, QBrush> brushes = d_ptr->theManager->brushes();
00142     QMap<QString, QBrush>::ConstIterator it = brushes.constBegin();
00143     while (it != brushes.constEnd()) {
00144         QString name = it.key();
00145         d_ptr->theManager->removeBrush(name);
00146 
00147         it++;
00148     }
00149 
00150     // fill up the manager from compiled resources or from brush folder here
00151     QDir brushDir(d_ptr->theBrushFolder);
00152     bool customBrushesExist = brushDir.exists();
00153     if (customBrushesExist) {
00154         // load brushes from brush folder
00155         QStringList nameFilters;
00156         nameFilters.append(QLatin1String("*.br"));
00157 
00158         QFileInfoList infos = brushDir.entryInfoList(nameFilters);
00159         QListIterator<QFileInfo> it(infos);
00160         while (it.hasNext()) {
00161             QFileInfo fi = it.next();
00162 
00163             QFile file(fi.absoluteFilePath());
00164             if (file.open(QIODevice::ReadOnly)) {
00165                 QByteArray contents = file.readAll();
00166                 file.close();
00167                 QDomDocument doc;
00168                 if (doc.setContent(contents)) {
00169                     QDomElement domElement = doc.documentElement();
00170 
00171                     QString name = domElement.attribute(QLatin1String("name"));
00172                     QString filename = fi.fileName();
00173 
00174                     QSimpleResource resource(d_ptr->theCore);
00175 
00176                     QDomElement brushElement = domElement.firstChildElement(QLatin1String("brush"));
00177                     DomBrush dom;
00178                     dom.read(brushElement);
00179                     QBrush br = resource.setupBrush(&dom);
00180 
00181                     d_ptr->theManager->addBrush(name, br);
00182                     d_ptr->theFileToBrush[filename] = name;
00183                     d_ptr->theBrushToFile[name] = filename;
00184                 }
00185             }
00186         }
00187     }
00188 
00189     connect(d_ptr->theManager, SIGNAL(brushAdded(const QString &, const QBrush &)),
00190             this, SLOT(brushAdded(const QString &, const QBrush &)));
00191     connect(d_ptr->theManager, SIGNAL(brushRemoved(const QString &)),
00192             this, SLOT(brushRemoved(const QString &)));
00193 
00194     if (!customBrushesExist) {
00195         // load brushes from resources
00196         QFile qrcFile(QLatin1String(":trolltech/brushes/defaultbrushes.xml"));
00197         if (qrcFile.open(QIODevice::ReadOnly)) {
00198             QByteArray contents = qrcFile.readAll();
00199             qrcFile.close();
00200             QDomDocument doc;
00201             if (doc.setContent(contents)) {
00202                 QDomElement domElement = doc.documentElement();
00203 
00204                 QDomElement descElement = domElement.firstChildElement(QLatin1String("description"));
00205                 while (!descElement.isNull()) {
00206                     QString name = descElement.attribute(QLatin1String("name"));
00207 
00208                     QSimpleResource resource(d_ptr->theCore);
00209 
00210                     QDomElement brushElement = descElement.firstChildElement(QLatin1String("brush"));
00211                     DomBrush dom;
00212                     dom.read(brushElement);
00213                     QBrush br = resource.setupBrush(&dom);
00214 
00215                     d_ptr->theManager->addBrush(name, br);
00216 
00217                     descElement = descElement.nextSiblingElement(QLatin1String("description"));
00218                 }
00219             }
00220         }
00221     }
00222 }
00223 
00224 #include "moc_brushmanagerproxy.cpp"

Generated on Thu Mar 15 12:00:59 2007 for Qt 4.2 User's Guide by  doxygen 1.5.1