source: trunk/tools/designer/src/lib/shared/qtresourceview.cpp@ 372

Last change on this file since 372 was 372, checked in by Dmitry A. Kuminov, 16 years ago

tools: Patched qdesigner to make it compile without Drag&Drop.

File size: 24.9 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the Qt Designer of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "abstractsettings_p.h"
43#include "qtresourceview_p.h"
44#include "qtresourcemodel_p.h"
45#include "qtresourceeditordialog_p.h"
46#include "iconloader_p.h"
47
48#include <QtDesigner/QDesignerFormEditorInterface>
49
50#include <QtGui/QToolBar>
51#include <QtGui/QAction>
52#include <QtGui/QSplitter>
53#include <QtGui/QTreeWidget>
54#include <QtGui/QListWidget>
55#include <QtGui/QHeaderView>
56#include <QtGui/QVBoxLayout>
57#include <QtGui/QPainter>
58#include <QtCore/QFileInfo>
59#include <QtCore/QDir>
60#include <QtCore/QQueue>
61#include <QtGui/QPainter>
62#include <QtGui/QDialogButtonBox>
63#include <QtGui/QPushButton>
64#include <QtGui/QMessageBox>
65#include <QtGui/QApplication>
66#include <QtGui/QClipboard>
67#include <QtGui/QMenu>
68#include <QtGui/QDrag>
69#include <QtCore/QMimeData>
70#include <QtXml/QDomDocument>
71
72QT_BEGIN_NAMESPACE
73
74static const char *elementResourceData = "resource";
75static const char *typeAttribute = "type";
76static const char *typeImage = "image";
77static const char *typeStyleSheet = "stylesheet";
78static const char *typeOther = "other";
79static const char *fileAttribute = "file";
80static const char *SplitterPosition = "SplitterPosition";
81static const char *Geometry = "Geometry";
82static const char *ResourceViewDialogC = "ResourceDialog";
83
84// ---------------- ResourceListWidget: A list widget that has drag enabled
85class ResourceListWidget : public QListWidget {
86public:
87 ResourceListWidget(QWidget *parent = 0);
88
89protected:
90 virtual void startDrag(Qt::DropActions supportedActions);
91};
92
93ResourceListWidget::ResourceListWidget(QWidget *parent) :
94 QListWidget(parent)
95{
96#ifndef QT_NO_DRAGANDDROP
97 setDragEnabled(true);
98#endif
99}
100
101void ResourceListWidget::startDrag(Qt::DropActions supportedActions)
102{
103 if (supportedActions == Qt::MoveAction)
104 return;
105
106 QListWidgetItem *item = currentItem();
107 if (!item)
108 return;
109
110#ifndef QT_NO_DRAGANDDROP
111 const QString filePath = item->data(Qt::UserRole).toString();
112 const QIcon icon = item->icon();
113
114 QMimeData *mimeData = new QMimeData;
115 const QtResourceView::ResourceType type = icon.isNull() ? QtResourceView::ResourceOther : QtResourceView::ResourceImage;
116 mimeData->setText(QtResourceView::encodeMimeData(type , filePath));
117
118 QDrag *drag = new QDrag(this);
119 if (!icon.isNull()) {
120 const QSize size = icon.actualSize(iconSize());
121 drag->setPixmap(icon.pixmap(size));
122 drag->setHotSpot(QPoint(size.width() / 2, size.height() / 2));
123 }
124
125 drag->setMimeData(mimeData);
126 drag->exec(Qt::CopyAction);
127#endif
128}
129
130/* TODO
131
132 1) load the icons in separate thread...Hmm..if Qt is configured with threads....
133*/
134
135// ---------------------------- QtResourceViewPrivate
136class QtResourceViewPrivate
137{
138 QtResourceView *q_ptr;
139 Q_DECLARE_PUBLIC(QtResourceView)
140public:
141 QtResourceViewPrivate(QDesignerFormEditorInterface *core);
142
143 void slotResourceSetActivated(QtResourceSet *resourceSet);
144 void slotCurrentPathChanged(QTreeWidgetItem *);
145 void slotCurrentResourceChanged(QListWidgetItem *);
146 void slotResourceActivated(QListWidgetItem *);
147 void slotEditResources();
148 void slotReloadResources();
149 void slotCopyResourcePath();
150 void slotListWidgetContextMenuRequested(const QPoint &pos);
151 void createPaths();
152 QTreeWidgetItem *createPath(const QString &path, QTreeWidgetItem *parent);
153 void createResources(const QString &path);
154 void storeExpansionState();
155 void applyExpansionState();
156 void restoreSettings();
157 void saveSettings();
158 void updateActions();
159
160 QPixmap makeThumbnail(const QPixmap &pix) const;
161
162 QDesignerFormEditorInterface *m_core;
163 QtResourceModel *m_resourceModel;
164 QToolBar *m_toolBar;
165 QTreeWidget *m_treeWidget;
166 QListWidget *m_listWidget;
167 QSplitter *m_splitter;
168 QMap<QString, QStringList> m_pathToContents; // full path to contents file names
169 QMap<QString, QTreeWidgetItem *> m_pathToItem;
170 QMap<QTreeWidgetItem *, QString> m_itemToPath;
171 QMap<QString, QListWidgetItem *> m_resourceToItem;
172 QMap<QListWidgetItem *, QString> m_itemToResource;
173 QAction *m_editResourcesAction;
174 QAction *m_reloadResourcesAction;
175 QAction *m_copyResourcePathAction;
176
177 QMap<QString, bool> m_expansionState;
178
179 bool m_ignoreGuiSignals;
180 QString m_settingsKey;
181 bool m_resourceEditingEnabled;
182};
183
184QtResourceViewPrivate::QtResourceViewPrivate(QDesignerFormEditorInterface *core) :
185 q_ptr(0),
186 m_core(core),
187 m_resourceModel(0),
188 m_toolBar(new QToolBar),
189 m_treeWidget(new QTreeWidget),
190 m_listWidget(new ResourceListWidget),
191 m_splitter(0),
192 m_editResourcesAction(0),
193 m_reloadResourcesAction(0),
194 m_copyResourcePathAction(0),
195 m_ignoreGuiSignals(false),
196 m_resourceEditingEnabled(true)
197{
198}
199
200void QtResourceViewPrivate::restoreSettings()
201{
202 if (m_settingsKey.isEmpty())
203 return;
204
205 QDesignerSettingsInterface *settings = m_core->settingsManager();
206 settings->beginGroup(m_settingsKey);
207
208 m_splitter->restoreState(settings->value(QLatin1String(SplitterPosition)).toByteArray());
209 settings->endGroup();
210}
211
212void QtResourceViewPrivate::saveSettings()
213{
214 if (m_settingsKey.isEmpty())
215 return;
216
217 QDesignerSettingsInterface *settings = m_core->settingsManager();
218 settings->beginGroup(m_settingsKey);
219
220 settings->setValue(QLatin1String(SplitterPosition), m_splitter->saveState());
221 settings->endGroup();
222}
223
224void QtResourceViewPrivate::slotEditResources()
225{
226 const QString selectedResource
227 = QtResourceEditorDialog::editResources(m_core, m_resourceModel,
228 m_core->dialogGui(), q_ptr);
229 if (!selectedResource.isEmpty())
230 q_ptr->selectResource(selectedResource);
231}
232
233void QtResourceViewPrivate::slotReloadResources()
234{
235 if (m_resourceModel) {
236 int errorCount;
237 QString errorMessages;
238 m_resourceModel->reload(&errorCount, &errorMessages);
239 if (errorCount)
240 QtResourceEditorDialog::displayResourceFailures(errorMessages, m_core->dialogGui(), q_ptr);
241 }
242}
243
244void QtResourceViewPrivate::slotCopyResourcePath()
245{
246 const QString path = q_ptr->selectedResource();
247 QClipboard *clipboard = QApplication::clipboard();
248 clipboard->setText(path);
249}
250
251void QtResourceViewPrivate::slotListWidgetContextMenuRequested(const QPoint &pos)
252{
253 QMenu menu(q_ptr);
254 menu.addAction(m_copyResourcePathAction);
255 menu.exec(m_listWidget->mapToGlobal(pos));
256}
257
258void QtResourceViewPrivate::storeExpansionState()
259{
260 QMapIterator<QString, QTreeWidgetItem *> it(m_pathToItem);
261 while (it.hasNext()) {
262 it.next();
263 m_expansionState[it.key()] = it.value()->isExpanded();
264 }
265}
266
267void QtResourceViewPrivate::applyExpansionState()
268{
269 QMapIterator<QString, QTreeWidgetItem *> it(m_pathToItem);
270 while (it.hasNext()) {
271 it.next();
272 it.value()->setExpanded(m_expansionState.value(it.key(), true));
273 }
274}
275
276QPixmap QtResourceViewPrivate::makeThumbnail(const QPixmap &pix) const
277{
278 int w = qMax(48, pix.width());
279 int h = qMax(48, pix.height());
280 QRect imgRect(0, 0, w, h);
281 QImage img(w, h, QImage::Format_ARGB32_Premultiplied);
282 img.fill(0);
283 if (!pix.isNull()) {
284 QRect r(0, 0, pix.width(), pix.height());
285 r.moveCenter(imgRect.center());
286 QPainter p(&img);
287 p.drawPixmap(r.topLeft(), pix);
288 }
289 return QPixmap::fromImage(img);
290}
291
292void QtResourceViewPrivate::updateActions()
293{
294 bool resourceActive = false;
295 if (m_resourceModel)
296 resourceActive = m_resourceModel->currentResourceSet();
297
298 m_editResourcesAction->setVisible(m_resourceEditingEnabled);
299 m_editResourcesAction->setEnabled(resourceActive);
300 m_reloadResourcesAction->setEnabled(resourceActive);
301}
302
303void QtResourceViewPrivate::slotResourceSetActivated(QtResourceSet *resourceSet)
304{
305 Q_UNUSED(resourceSet)
306
307 updateActions();
308
309 storeExpansionState();
310 const QString currentPath = m_itemToPath.value(m_treeWidget->currentItem());
311 const QString currentResource = m_itemToResource.value(m_listWidget->currentItem());
312 m_treeWidget->clear();
313 m_pathToContents.clear();
314 m_pathToItem.clear();
315 m_itemToPath.clear();
316 m_listWidget->clear();
317 m_resourceToItem.clear();
318 m_itemToResource.clear();
319
320 createPaths();
321 applyExpansionState();
322
323 if (!currentResource.isEmpty())
324 q_ptr->selectResource(currentResource);
325 else if (!currentPath.isEmpty())
326 q_ptr->selectResource(currentPath);
327}
328
329void QtResourceViewPrivate::slotCurrentPathChanged(QTreeWidgetItem *item)
330{
331 if (m_ignoreGuiSignals)
332 return;
333
334 m_listWidget->clear();
335 m_resourceToItem.clear();
336 m_itemToResource.clear();
337
338 if (!item)
339 return;
340
341 const QString currentPath = m_itemToPath.value(item);
342 createResources(currentPath);
343}
344
345void QtResourceViewPrivate::slotCurrentResourceChanged(QListWidgetItem *item)
346{
347 m_copyResourcePathAction->setEnabled(item);
348 if (m_ignoreGuiSignals)
349 return;
350
351 emit q_ptr->resourceSelected(m_itemToResource.value(item));
352}
353
354void QtResourceViewPrivate::slotResourceActivated(QListWidgetItem *item)
355{
356 if (m_ignoreGuiSignals)
357 return;
358
359 emit q_ptr->resourceActivated(m_itemToResource.value(item));
360}
361
362void QtResourceViewPrivate::createPaths()
363{
364 if (!m_resourceModel)
365 return;
366
367 const QString root(QLatin1Char(':'));
368
369 QMap<QString, QString> pathToParentPath; // full path to full parent path
370 QMap<QString, QStringList> pathToSubPaths; // full path to full sub paths
371
372 QMap<QString, QString> contents = m_resourceModel->contents();
373 QMapIterator<QString, QString> itContents(contents);
374 while (itContents.hasNext()) {
375 const QString filePath = itContents.next().key();
376 const QFileInfo fi(filePath);
377 QString dirPath = fi.absolutePath();
378 m_pathToContents[dirPath].append(fi.fileName());
379 while (!pathToParentPath.contains(dirPath) && dirPath != root) {
380 const QFileInfo fd(dirPath);
381 const QString parentDirPath = fd.absolutePath();
382 pathToParentPath[dirPath] = parentDirPath;
383 pathToSubPaths[parentDirPath].append(dirPath);
384 dirPath = parentDirPath;
385 }
386 }
387
388 QQueue<QPair<QString, QTreeWidgetItem *> > pathToParentItemQueue;
389 pathToParentItemQueue.enqueue(qMakePair(root, static_cast<QTreeWidgetItem *>(0)));
390 while (!pathToParentItemQueue.isEmpty()) {
391 QPair<QString, QTreeWidgetItem *> pathToParentItem = pathToParentItemQueue.dequeue();
392 const QString path = pathToParentItem.first;
393 QTreeWidgetItem *item = createPath(path, pathToParentItem.second);
394 QStringList subPaths = pathToSubPaths.value(path);
395 QStringListIterator itSubPaths(subPaths);
396 while (itSubPaths.hasNext())
397 pathToParentItemQueue.enqueue(qMakePair(itSubPaths.next(), item));
398 }
399}
400
401QTreeWidgetItem *QtResourceViewPrivate::createPath(const QString &path, QTreeWidgetItem *parent)
402{
403 QTreeWidgetItem *item = 0;
404 if (parent)
405 item = new QTreeWidgetItem(parent);
406 else
407 item = new QTreeWidgetItem(m_treeWidget);
408 m_pathToItem[path] = item;
409 m_itemToPath[item] = path;
410 QString substPath;
411 if (parent) {
412 QFileInfo di(path);
413 substPath = di.fileName();
414 } else {
415 substPath = QLatin1String("<resource root>");
416 }
417 item->setText(0, substPath);
418 item->setToolTip(0, path);
419 return item;
420}
421
422void QtResourceViewPrivate::createResources(const QString &path)
423{
424 QDir dir(path);
425 QStringList files = m_pathToContents.value(path);
426 QStringListIterator it(files);
427 while (it.hasNext()) {
428 QString file = it.next();
429 QString filePath = dir.absoluteFilePath(file);
430 QFileInfo fi(filePath);
431 if (fi.isFile()) {
432 QListWidgetItem *item = new QListWidgetItem(fi.fileName(), m_listWidget);
433 const QPixmap pix = QPixmap(filePath);
434 if (pix.isNull()) {
435 item->setToolTip(filePath);
436 } else {
437 item->setIcon(QIcon(makeThumbnail(pix)));
438 const QSize size = pix.size();
439 item->setToolTip(QtResourceView::tr("Size: %1 x %2\n%3").arg(size.width()).arg(size.height()).arg(filePath));
440 }
441 item->setFlags(item->flags() | Qt::ItemIsDragEnabled);
442 item->setData(Qt::UserRole, filePath);
443 m_itemToResource[item] = filePath;
444 m_resourceToItem[filePath] = item;
445 }
446 }
447}
448
449// -------------- QtResourceView
450
451QtResourceView::QtResourceView(QDesignerFormEditorInterface *core, QWidget *parent) :
452 QWidget(parent),
453 d_ptr(new QtResourceViewPrivate(core))
454{
455 d_ptr->q_ptr = this;
456
457 d_ptr->m_editResourcesAction = new QAction(qdesigner_internal::createIconSet(QLatin1String("edit.png")), tr("Edit Resources..."), this);
458 d_ptr->m_toolBar->addAction(d_ptr->m_editResourcesAction);
459 connect(d_ptr->m_editResourcesAction, SIGNAL(triggered()), this, SLOT(slotEditResources()));
460 d_ptr->m_editResourcesAction->setEnabled(false);
461
462 d_ptr->m_reloadResourcesAction = new QAction(qdesigner_internal::createIconSet(QLatin1String("reload.png")), tr("Reload"), this);
463 d_ptr->m_toolBar->addAction(d_ptr->m_reloadResourcesAction);
464 connect(d_ptr->m_reloadResourcesAction, SIGNAL(triggered()), this, SLOT(slotReloadResources()));
465 d_ptr->m_reloadResourcesAction->setEnabled(false);
466
467 d_ptr->m_copyResourcePathAction = new QAction(qdesigner_internal::createIconSet(QLatin1String("editcopy.png")), tr("Copy Path"), this);
468 connect(d_ptr->m_copyResourcePathAction, SIGNAL(triggered()), this, SLOT(slotCopyResourcePath()));
469 d_ptr->m_copyResourcePathAction->setEnabled(false);
470
471 d_ptr->m_splitter = new QSplitter;
472 d_ptr->m_splitter->setChildrenCollapsible(false);
473 d_ptr->m_splitter->addWidget(d_ptr->m_treeWidget);
474 d_ptr->m_splitter->addWidget(d_ptr->m_listWidget);
475
476 QLayout *layout = new QVBoxLayout(this);
477 layout->setMargin(0);
478 layout->setSpacing(0);
479 layout->addWidget(d_ptr->m_toolBar);
480 layout->addWidget(d_ptr->m_splitter);
481
482 d_ptr->m_treeWidget->setColumnCount(1);
483 d_ptr->m_treeWidget->header()->hide();
484 d_ptr->m_treeWidget->setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding));
485
486 d_ptr->m_listWidget->setViewMode(QListView::IconMode);
487 d_ptr->m_listWidget->setResizeMode(QListView::Adjust);
488 d_ptr->m_listWidget->setIconSize(QSize(48, 48));
489 d_ptr->m_listWidget->setGridSize(QSize(64, 64));
490
491 connect(d_ptr->m_treeWidget, SIGNAL(currentItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)),
492 this, SLOT(slotCurrentPathChanged(QTreeWidgetItem *)));
493 connect(d_ptr->m_listWidget, SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem *)),
494 this, SLOT(slotCurrentResourceChanged(QListWidgetItem *)));
495 connect(d_ptr->m_listWidget, SIGNAL(itemActivated(QListWidgetItem *)),
496 this, SLOT(slotResourceActivated(QListWidgetItem *)));
497 d_ptr->m_listWidget->setContextMenuPolicy(Qt::CustomContextMenu);
498 connect(d_ptr->m_listWidget, SIGNAL(customContextMenuRequested(QPoint)),
499 this, SLOT(slotListWidgetContextMenuRequested(QPoint)));
500}
501
502QtResourceView::~QtResourceView()
503{
504 if (!d_ptr->m_settingsKey.isEmpty())
505 d_ptr->saveSettings();
506
507 delete d_ptr;
508}
509
510bool QtResourceView::event(QEvent *event)
511{
512 if (event->type() == QEvent::Show) {
513 d_ptr->m_listWidget->scrollToItem(d_ptr->m_listWidget->currentItem());
514 d_ptr->m_treeWidget->scrollToItem(d_ptr->m_treeWidget->currentItem());
515 }
516 return QWidget::event(event);
517}
518
519QtResourceModel *QtResourceView::model() const
520{
521 return d_ptr->m_resourceModel;
522}
523
524QString QtResourceView::selectedResource() const
525{
526 QListWidgetItem *item = d_ptr->m_listWidget->currentItem();
527 return d_ptr->m_itemToResource.value(item);
528}
529
530void QtResourceView::selectResource(const QString &resource)
531{
532 QFileInfo fi(resource);
533 QDir dir = fi.absoluteDir();
534 if (fi.isDir())
535 dir = QDir(resource);
536 QString dirPath = dir.absolutePath();
537 QMap<QString, QTreeWidgetItem *>::const_iterator it;
538 while ((it = d_ptr->m_pathToItem.find(dirPath)) == d_ptr->m_pathToItem.constEnd()) {
539 if (!dir.cdUp())
540 break;
541 dirPath = dir.absolutePath();
542 }
543 if (it != d_ptr->m_pathToItem.constEnd()) {
544 QTreeWidgetItem *treeItem = it.value();
545 d_ptr->m_treeWidget->setCurrentItem(treeItem);
546 d_ptr->m_treeWidget->scrollToItem(treeItem);
547 // expand all up to current one is done by qt
548 // list widget is already propagated (currrent changed was sent by qt)
549 QListWidgetItem *item = d_ptr->m_resourceToItem.value(resource);
550 if (item) {
551 d_ptr->m_listWidget->setCurrentItem(item);
552 d_ptr->m_listWidget->scrollToItem(item);
553 }
554 }
555}
556
557QString QtResourceView::settingsKey() const
558{
559 return d_ptr->m_settingsKey;
560}
561
562void QtResourceView::setSettingsKey(const QString &key)
563{
564 if (d_ptr->m_settingsKey == key)
565 return;
566
567 d_ptr->m_settingsKey = key;
568
569 if (key.isEmpty())
570 return;
571
572 d_ptr->restoreSettings();
573}
574
575void QtResourceView::setResourceModel(QtResourceModel *model)
576{
577 if (d_ptr->m_resourceModel) {
578 disconnect(d_ptr->m_resourceModel, SIGNAL(resourceSetActivated(QtResourceSet *, bool)),
579 this, SLOT(slotResourceSetActivated(QtResourceSet *)));
580 }
581
582 // clear here
583 d_ptr->m_treeWidget->clear();
584 d_ptr->m_listWidget->clear();
585
586 d_ptr->m_resourceModel = model;
587
588 if (!d_ptr->m_resourceModel)
589 return;
590
591 connect(d_ptr->m_resourceModel, SIGNAL(resourceSetActivated(QtResourceSet *, bool)),
592 this, SLOT(slotResourceSetActivated(QtResourceSet *)));
593
594 // fill new here
595 d_ptr->slotResourceSetActivated(d_ptr->m_resourceModel->currentResourceSet());
596}
597
598bool QtResourceView::isResourceEditingEnabled() const
599{
600 return d_ptr->m_resourceEditingEnabled;
601}
602
603void QtResourceView::setResourceEditingEnabled(bool enable)
604{
605 d_ptr->m_resourceEditingEnabled = enable;
606 d_ptr->updateActions();
607}
608
609void QtResourceView::setDragEnabled(bool dragEnabled)
610{
611#ifndef QT_NO_DRAGANDDROP
612 d_ptr->m_listWidget->setDragEnabled(dragEnabled);
613#endif
614}
615
616bool QtResourceView::dragEnabled() const
617{
618#ifndef QT_NO_DRAGANDDROP
619 return d_ptr->m_listWidget->dragEnabled();
620#endif
621}
622
623QString QtResourceView::encodeMimeData(ResourceType resourceType, const QString &path)
624{
625 QDomDocument doc;
626 QDomElement elem = doc.createElement(QLatin1String(elementResourceData));
627 switch (resourceType) {
628 case ResourceImage:
629 elem.setAttribute(QLatin1String(typeAttribute), QLatin1String(typeImage));
630 break;
631 case ResourceStyleSheet:
632 elem.setAttribute(QLatin1String(typeAttribute), QLatin1String(typeStyleSheet));
633 break;
634 case ResourceOther:
635 elem.setAttribute(QLatin1String(typeAttribute), QLatin1String(typeOther));
636 break;
637 }
638 elem.setAttribute(QLatin1String(fileAttribute), path);
639 doc.appendChild(elem);
640 return doc.toString();
641}
642
643bool QtResourceView::decodeMimeData(const QMimeData *md, ResourceType *t, QString *file)
644{
645 return md->hasText() ? decodeMimeData(md->text(), t, file) : false;
646}
647
648bool QtResourceView::decodeMimeData(const QString &text, ResourceType *t, QString *file)
649{
650
651 const QString docElementName = QLatin1String(elementResourceData);
652 static const QString docElementString = QLatin1Char('<') + docElementName;
653
654 if (text.isEmpty() || text.indexOf(docElementString) == -1)
655 return false;
656
657 QDomDocument doc;
658 if (!doc.setContent(text))
659 return false;
660
661 const QDomElement domElement = doc.documentElement();
662 if (domElement.tagName() != docElementName)
663 return false;
664
665 if (t) {
666 const QString typeAttr = QLatin1String(typeAttribute);
667 if (domElement.hasAttribute (typeAttr)) {
668 const QString typeValue = domElement.attribute(typeAttr, QLatin1String(typeOther));
669 if (typeValue == QLatin1String(typeImage)) {
670 *t = ResourceImage;
671 } else {
672 *t = typeValue == QLatin1String(typeStyleSheet) ? ResourceStyleSheet : ResourceOther;
673 }
674 }
675 }
676 if (file) {
677 const QString fileAttr = QLatin1String(fileAttribute);
678 if (domElement.hasAttribute(fileAttr)) {
679 *file = domElement.attribute(fileAttr, QString());
680 } else {
681 file->clear();
682 }
683 }
684 return true;
685}
686
687// ---------------------------- QtResourceViewDialogPrivate
688
689class QtResourceViewDialogPrivate
690{
691 QtResourceViewDialog *q_ptr;
692 Q_DECLARE_PUBLIC(QtResourceViewDialog)
693public:
694 QtResourceViewDialogPrivate(QDesignerFormEditorInterface *core);
695
696 void slotResourceSelected(const QString &resource) { setOkButtonEnabled(!resource.isEmpty()); }
697 void setOkButtonEnabled(bool v) { m_box->button(QDialogButtonBox::Ok)->setEnabled(v); }
698
699 QDesignerFormEditorInterface *m_core;
700 QtResourceView *m_view;
701 QDialogButtonBox *m_box;
702};
703
704QtResourceViewDialogPrivate::QtResourceViewDialogPrivate(QDesignerFormEditorInterface *core) :
705 q_ptr(0),
706 m_core(core),
707 m_view(new QtResourceView(core)),
708 m_box(new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel))
709{
710 m_view->setSettingsKey(QLatin1String(ResourceViewDialogC));
711}
712
713// ------------ QtResourceViewDialog
714QtResourceViewDialog::QtResourceViewDialog(QDesignerFormEditorInterface *core, QWidget *parent) :
715 QDialog(parent),
716 d_ptr(new QtResourceViewDialogPrivate(core))
717{
718 setWindowTitle(tr("Select Resource"));
719 setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
720 d_ptr->q_ptr = this;
721 QVBoxLayout *layout = new QVBoxLayout(this);
722 layout->addWidget(d_ptr->m_view);
723 layout->addWidget(d_ptr->m_box);
724 connect(d_ptr->m_box, SIGNAL(accepted()), this, SLOT(accept()));
725 connect(d_ptr->m_box, SIGNAL(rejected()), this, SLOT(reject()));
726 connect(d_ptr->m_view, SIGNAL(resourceActivated(QString)), this, SLOT(accept()));
727 connect(d_ptr->m_view, SIGNAL(resourceSelected(QString)), this, SLOT(slotResourceSelected(QString)));
728 d_ptr->setOkButtonEnabled(false);
729 d_ptr->m_view->setResourceModel(core->resourceModel());
730
731 QDesignerSettingsInterface *settings = core->settingsManager();
732 settings->beginGroup(QLatin1String(ResourceViewDialogC));
733
734 if (settings->contains(QLatin1String(Geometry)))
735 setGeometry(settings->value(QLatin1String(Geometry)).toRect());
736
737 settings->endGroup();
738}
739
740QtResourceViewDialog::~QtResourceViewDialog()
741{
742 QDesignerSettingsInterface *settings = d_ptr->m_core->settingsManager();
743 settings->beginGroup(QLatin1String(ResourceViewDialogC));
744
745 settings->setValue(QLatin1String(Geometry), geometry());
746
747 settings->endGroup();
748
749 delete d_ptr;
750}
751
752QString QtResourceViewDialog::selectedResource() const
753{
754 return d_ptr->m_view->selectedResource();
755}
756
757void QtResourceViewDialog::selectResource(const QString &path)
758{
759 d_ptr->m_view->selectResource(path);
760}
761
762bool QtResourceViewDialog::isResourceEditingEnabled() const
763{
764 return d_ptr->m_view->isResourceEditingEnabled();
765}
766
767void QtResourceViewDialog::setResourceEditingEnabled(bool enable)
768{
769 d_ptr->m_view->setResourceEditingEnabled(enable);
770}
771
772QT_END_NAMESPACE
773
774#include "moc_qtresourceview_p.cpp"
Note: See TracBrowser for help on using the repository browser.