~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to tools/designer/src/lib/shared/qtresourceview.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Alessandro Ghersi
  • Date: 2009-11-02 18:30:08 UTC
  • mfrom: (1.2.2 upstream)
  • mto: (15.2.5 experimental)
  • mto: This revision was merged to the branch mainline in revision 88.
  • Revision ID: james.westby@ubuntu.com-20091102183008-b6a4gcs128mvfb3m
Tags: upstream-4.6.0~beta1
ImportĀ upstreamĀ versionĀ 4.6.0~beta1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/****************************************************************************
2
2
**
3
3
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
 
4
** All rights reserved.
4
5
** Contact: Nokia Corporation (qt-info@nokia.com)
5
6
**
6
7
** This file is part of the Qt Designer of the Qt Toolkit.
7
8
**
8
9
** $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.
 
10
** No Commercial Usage
 
11
** This file contains pre-release code and may not be distributed.
 
12
** You may use this file in accordance with the terms and conditions
 
13
** contained in the Technology Preview License Agreement accompanying
 
14
** this package.
14
15
**
15
16
** GNU Lesser General Public License Usage
16
17
** Alternatively, this file may be used under the terms of the GNU Lesser
20
21
** ensure the GNU Lesser General Public License version 2.1 requirements
21
22
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22
23
**
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 http://www.qtsoftware.com/contact.
 
24
** In addition, as a special exception, Nokia gives you certain additional
 
25
** rights.  These rights are described in the Nokia Qt LGPL Exception
 
26
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
 
27
**
 
28
** If you have questions regarding the use of this file, please contact
 
29
** Nokia at qt-info@nokia.com.
 
30
**
 
31
**
 
32
**
 
33
**
 
34
**
 
35
**
 
36
**
 
37
**
38
38
** $QT_END_LICENSE$
39
39
**
40
40
****************************************************************************/
44
44
#include "qtresourcemodel_p.h"
45
45
#include "qtresourceeditordialog_p.h"
46
46
#include "iconloader_p.h"
 
47
#include "filterwidget_p.h" // For FilterWidget
47
48
 
48
49
#include <QtDesigner/QDesignerFormEditorInterface>
49
50
 
144
145
    void slotReloadResources();
145
146
    void slotCopyResourcePath();
146
147
    void slotListWidgetContextMenuRequested(const QPoint &pos);
 
148
    void slotFilterChanged(const QString &pattern);
147
149
    void createPaths();
148
150
    QTreeWidgetItem *createPath(const QString &path, QTreeWidgetItem *parent);
149
151
    void createResources(const QString &path);
152
154
    void restoreSettings();
153
155
    void saveSettings();
154
156
    void updateActions();
 
157
    void filterOutResources();
155
158
 
156
159
    QPixmap makeThumbnail(const QPixmap &pix) const;
157
160
 
158
161
    QDesignerFormEditorInterface *m_core;
159
162
    QtResourceModel *m_resourceModel;
160
163
    QToolBar *m_toolBar;
 
164
    qdesigner_internal::FilterWidget *m_filterWidget;
161
165
    QTreeWidget *m_treeWidget;
162
166
    QListWidget *m_listWidget;
163
167
    QSplitter *m_splitter;
164
 
    QMap<QString, QStringList>       m_pathToContents; // full path to contents file names
 
168
    QMap<QString, QStringList>       m_pathToContents; // full path to contents file names (full path to its resource filenames)
 
169
    QMap<QString, QString>           m_pathToParentPath; // full path to full parent path
 
170
    QMap<QString, QStringList>       m_pathToSubPaths; // full path to full sub paths
165
171
    QMap<QString, QTreeWidgetItem *> m_pathToItem;
166
172
    QMap<QTreeWidgetItem *, QString> m_itemToPath;
167
173
    QMap<QString, QListWidgetItem *> m_resourceToItem;
175
181
    bool m_ignoreGuiSignals;
176
182
    QString m_settingsKey;
177
183
    bool m_resourceEditingEnabled;
 
184
    QString m_filterPattern;
178
185
};
179
186
 
180
187
QtResourceViewPrivate::QtResourceViewPrivate(QDesignerFormEditorInterface *core) :
251
258
    menu.exec(m_listWidget->mapToGlobal(pos));
252
259
}
253
260
 
 
261
void QtResourceViewPrivate::slotFilterChanged(const QString &pattern)
 
262
{
 
263
    m_filterPattern = pattern;
 
264
    filterOutResources();
 
265
}
 
266
 
254
267
void QtResourceViewPrivate::storeExpansionState()
255
268
{
256
269
    QMapIterator<QString, QTreeWidgetItem *> it(m_pathToItem);
294
307
    m_editResourcesAction->setVisible(m_resourceEditingEnabled);
295
308
    m_editResourcesAction->setEnabled(resourceActive);
296
309
    m_reloadResourcesAction->setEnabled(resourceActive);
 
310
    m_filterWidget->setEnabled(resourceActive);
297
311
}
298
312
 
299
313
void QtResourceViewPrivate::slotResourceSetActivated(QtResourceSet *resourceSet)
307
321
    const QString currentResource = m_itemToResource.value(m_listWidget->currentItem());
308
322
    m_treeWidget->clear();
309
323
    m_pathToContents.clear();
 
324
    m_pathToParentPath.clear();
 
325
    m_pathToSubPaths.clear();
310
326
    m_pathToItem.clear();
311
327
    m_itemToPath.clear();
312
328
    m_listWidget->clear();
320
336
        q_ptr->selectResource(currentResource);
321
337
    else if (!currentPath.isEmpty())
322
338
        q_ptr->selectResource(currentPath);
 
339
    filterOutResources();
323
340
}
324
341
 
325
342
void QtResourceViewPrivate::slotCurrentPathChanged(QTreeWidgetItem *item)
362
379
 
363
380
    const QString root(QLatin1Char(':'));
364
381
 
365
 
    QMap<QString, QString> pathToParentPath; // full path to full parent path
366
 
    QMap<QString, QStringList> pathToSubPaths; // full path to full sub paths
367
 
 
368
382
    QMap<QString, QString> contents = m_resourceModel->contents();
369
383
    QMapIterator<QString, QString> itContents(contents);
370
384
    while (itContents.hasNext()) {
372
386
        const QFileInfo fi(filePath);
373
387
        QString dirPath = fi.absolutePath();
374
388
        m_pathToContents[dirPath].append(fi.fileName());
375
 
        while (!pathToParentPath.contains(dirPath) && dirPath != root) {
 
389
        while (!m_pathToParentPath.contains(dirPath) && dirPath != root) { // create all parent paths
376
390
            const QFileInfo fd(dirPath);
377
391
            const QString parentDirPath = fd.absolutePath();
378
 
            pathToParentPath[dirPath] = parentDirPath;
379
 
            pathToSubPaths[parentDirPath].append(dirPath);
 
392
            m_pathToParentPath[dirPath] = parentDirPath;
 
393
            m_pathToSubPaths[parentDirPath].append(dirPath);
380
394
            dirPath = parentDirPath;
381
395
        }
382
396
    }
387
401
        QPair<QString, QTreeWidgetItem *> pathToParentItem = pathToParentItemQueue.dequeue();
388
402
        const QString path = pathToParentItem.first;
389
403
        QTreeWidgetItem *item = createPath(path, pathToParentItem.second);
390
 
        QStringList subPaths = pathToSubPaths.value(path);
 
404
        QStringList subPaths = m_pathToSubPaths.value(path);
391
405
        QStringListIterator itSubPaths(subPaths);
392
406
        while (itSubPaths.hasNext())
393
407
            pathToParentItemQueue.enqueue(qMakePair(itSubPaths.next(), item));
394
408
    }
395
409
}
396
410
 
 
411
void QtResourceViewPrivate::filterOutResources()
 
412
{
 
413
    QMap<QString, bool> pathToMatchingContents; // true means the path has any matching contents
 
414
    QMap<QString, bool> pathToVisible; // true means the path has to be shown
 
415
 
 
416
    // 1) we go from root path recursively.
 
417
    // 2) we check every path if it contains at least one matching resource - if empty we add it
 
418
    //                 to pathToMatchingContents and pathToVisible with false, if non empty
 
419
    //                 we add it with true and change every parent path in pathToVisible to true.
 
420
    // 3) we hide these items which has pathToVisible value false.
 
421
 
 
422
    const bool matchAll = m_filterPattern.isEmpty();
 
423
    const QString root(QLatin1Char(':'));
 
424
 
 
425
    QQueue<QString> pathQueue;
 
426
    pathQueue.enqueue(root);
 
427
    while (!pathQueue.isEmpty()) {
 
428
        const QString path = pathQueue.dequeue();
 
429
 
 
430
        QStringList fileNames = m_pathToContents.value(path);
 
431
        QStringListIterator it(fileNames);
 
432
        bool hasContents = matchAll;
 
433
        if (!matchAll) { // the case filter is not empty - we check if the path contains anything
 
434
            while (it.hasNext()) {
 
435
                QString fileName = it.next();
 
436
                hasContents = fileName.contains(m_filterPattern, Qt::CaseInsensitive);
 
437
                if (hasContents) // the path contains at least one resource which matches the filter
 
438
                    break;
 
439
            }
 
440
        }
 
441
 
 
442
        pathToMatchingContents[path] = hasContents;
 
443
        pathToVisible[path] = hasContents;
 
444
 
 
445
        if (hasContents) { // if the path is going to be shown we need to show all its parent paths
 
446
            QString parentPath = m_pathToParentPath.value(path);
 
447
            while (!parentPath.isEmpty()) {
 
448
                QString p = parentPath;
 
449
                if (pathToVisible.value(p)) // parent path is already shown, we break the loop
 
450
                    break;
 
451
                pathToVisible[p] = true;
 
452
                parentPath = m_pathToParentPath.value(p);
 
453
            }
 
454
        }
 
455
 
 
456
        QStringList subPaths = m_pathToSubPaths.value(path); // we do the same for children paths
 
457
        QStringListIterator itSubPaths(subPaths);
 
458
        while (itSubPaths.hasNext())
 
459
            pathQueue.enqueue(itSubPaths.next());
 
460
    }
 
461
 
 
462
    // we setup here new path and resource to be activated
 
463
    const QString currentPath = m_itemToPath.value(m_treeWidget->currentItem());
 
464
    QString newCurrentPath = currentPath;
 
465
    QString currentResource = m_itemToResource.value(m_listWidget->currentItem());
 
466
    if (!matchAll) {
 
467
        bool searchForNewPathWithContents = true;
 
468
 
 
469
        if (!currentPath.isEmpty()) { // if the currentPath is empty we will search for a new path too
 
470
            QMap<QString, bool>::ConstIterator it = pathToMatchingContents.constFind(currentPath);
 
471
            if (it != pathToMatchingContents.constEnd() && it.value()) // the current item has contents, we don't need to search for another path
 
472
                searchForNewPathWithContents = false;
 
473
        }
 
474
 
 
475
        if (searchForNewPathWithContents) {
 
476
            // we find the first path with the matching contents
 
477
            QMap<QString, bool>::ConstIterator itContents = pathToMatchingContents.constBegin();
 
478
            while (itContents != pathToMatchingContents.constEnd()) {
 
479
                if (itContents.value()) {
 
480
                    newCurrentPath = itContents.key(); // the new path will be activated
 
481
                    break;
 
482
                }
 
483
 
 
484
                itContents++;
 
485
            }
 
486
        }
 
487
 
 
488
        QFileInfo fi(currentResource);
 
489
        if (!fi.fileName().contains(m_filterPattern, Qt::CaseInsensitive)) { // the case when the current resource is filtered out
 
490
            const QStringList fileNames = m_pathToContents.value(newCurrentPath);
 
491
            QStringListIterator it(fileNames);
 
492
            while (it.hasNext()) { // we try to select the first matching resource from the newCurrentPath
 
493
                QString fileName = it.next();
 
494
                if (fileName.contains(m_filterPattern, Qt::CaseInsensitive)) {
 
495
                    QDir dirPath(newCurrentPath);
 
496
                    currentResource = dirPath.absoluteFilePath(fileName); // the new resource inside newCurrentPath will be activated
 
497
                    break;
 
498
                }
 
499
            }
 
500
        }
 
501
    }
 
502
 
 
503
    QTreeWidgetItem *newCurrentItem = m_pathToItem.value(newCurrentPath);
 
504
    if (currentPath != newCurrentPath)
 
505
        m_treeWidget->setCurrentItem(newCurrentItem);
 
506
    else
 
507
        slotCurrentPathChanged(newCurrentItem); // trigger filtering on the current path
 
508
 
 
509
    QListWidgetItem *currentResourceItem = m_resourceToItem.value(currentResource);
 
510
    if (currentResourceItem) {
 
511
        m_listWidget->setCurrentItem(currentResourceItem);
 
512
        m_listWidget->scrollToItem(currentResourceItem);
 
513
    }
 
514
 
 
515
    QMapIterator<QString, bool> it(pathToVisible); // hide all paths filtered out
 
516
    while (it.hasNext()) {
 
517
        const QString path = it.next().key();
 
518
        QTreeWidgetItem *item = m_pathToItem.value(path);
 
519
        if (item)
 
520
            item->setHidden(!it.value());
 
521
    }
 
522
}
 
523
 
397
524
QTreeWidgetItem *QtResourceViewPrivate::createPath(const QString &path, QTreeWidgetItem *parent)
398
525
{
399
526
    QTreeWidgetItem *item = 0;
417
544
 
418
545
void QtResourceViewPrivate::createResources(const QString &path)
419
546
{
 
547
    const bool matchAll = m_filterPattern.isEmpty();
 
548
 
420
549
    QDir dir(path);
421
 
    QStringList files = m_pathToContents.value(path);
422
 
    QStringListIterator it(files);
 
550
    QStringList fileNames = m_pathToContents.value(path);
 
551
    QStringListIterator it(fileNames);
423
552
    while (it.hasNext()) {
424
 
        QString file = it.next();
425
 
        QString filePath = dir.absoluteFilePath(file);
426
 
        QFileInfo fi(filePath);
427
 
        if (fi.isFile()) {
428
 
            QListWidgetItem *item = new QListWidgetItem(fi.fileName(), m_listWidget);
429
 
            const QPixmap pix = QPixmap(filePath);
430
 
            if (pix.isNull()) {
431
 
                item->setToolTip(filePath);
432
 
            } else {
433
 
                item->setIcon(QIcon(makeThumbnail(pix)));
434
 
                const QSize size = pix.size();
435
 
                item->setToolTip(QtResourceView::tr("Size: %1 x %2\n%3").arg(size.width()).arg(size.height()).arg(filePath));
 
553
        QString fileName = it.next();
 
554
        const bool showProperty = matchAll || fileName.contains(m_filterPattern, Qt::CaseInsensitive);
 
555
        if (showProperty) {
 
556
            QString filePath = dir.absoluteFilePath(fileName);
 
557
            QFileInfo fi(filePath);
 
558
            if (fi.isFile()) {
 
559
                QListWidgetItem *item = new QListWidgetItem(fi.fileName(), m_listWidget);
 
560
                const QPixmap pix = QPixmap(filePath);
 
561
                if (pix.isNull()) {
 
562
                    item->setToolTip(filePath);
 
563
                } else {
 
564
                    item->setIcon(QIcon(makeThumbnail(pix)));
 
565
                    const QSize size = pix.size();
 
566
                    item->setToolTip(QtResourceView::tr("Size: %1 x %2\n%3").arg(size.width()).arg(size.height()).arg(filePath));
 
567
                }
 
568
                item->setFlags(item->flags() | Qt::ItemIsDragEnabled);
 
569
                item->setData(Qt::UserRole, filePath);
 
570
                m_itemToResource[item] = filePath;
 
571
                m_resourceToItem[filePath] = item;
436
572
            }
437
 
            item->setFlags(item->flags() | Qt::ItemIsDragEnabled);
438
 
            item->setData(Qt::UserRole, filePath);
439
 
            m_itemToResource[item] = filePath;
440
 
            m_resourceToItem[filePath] = item;
441
573
        }
442
574
    }
443
575
}
450
582
{
451
583
    d_ptr->q_ptr = this;
452
584
 
453
 
    d_ptr->m_editResourcesAction = new QAction(qdesigner_internal::createIconSet(QLatin1String("edit.png")), tr("Edit Resources..."), this);
 
585
    QIcon editIcon = QIcon::fromTheme("document-properties", qdesigner_internal::createIconSet(QLatin1String("edit.png")));
 
586
    d_ptr->m_editResourcesAction = new QAction(editIcon, tr("Edit Resources..."), this);
454
587
    d_ptr->m_toolBar->addAction(d_ptr->m_editResourcesAction);
455
588
    connect(d_ptr->m_editResourcesAction, SIGNAL(triggered()), this, SLOT(slotEditResources()));
456
589
    d_ptr->m_editResourcesAction->setEnabled(false);
457
590
 
458
 
    d_ptr->m_reloadResourcesAction = new QAction(qdesigner_internal::createIconSet(QLatin1String("reload.png")), tr("Reload"), this);
 
591
    QIcon refreshIcon = QIcon::fromTheme("view-refresh", qdesigner_internal::createIconSet(QLatin1String("reload.png")));
 
592
    d_ptr->m_reloadResourcesAction = new QAction(refreshIcon, tr("Reload"), this);
 
593
 
459
594
    d_ptr->m_toolBar->addAction(d_ptr->m_reloadResourcesAction);
460
595
    connect(d_ptr->m_reloadResourcesAction, SIGNAL(triggered()), this, SLOT(slotReloadResources()));
461
596
    d_ptr->m_reloadResourcesAction->setEnabled(false);
462
597
 
463
 
    d_ptr->m_copyResourcePathAction = new QAction(qdesigner_internal::createIconSet(QLatin1String("editcopy.png")), tr("Copy Path"), this);
 
598
    QIcon copyIcon = QIcon::fromTheme("edit-copy", qdesigner_internal::createIconSet(QLatin1String("editcopy.png")));
 
599
    d_ptr->m_copyResourcePathAction = new QAction(copyIcon, tr("Copy Path"), this);
464
600
    connect(d_ptr->m_copyResourcePathAction, SIGNAL(triggered()), this, SLOT(slotCopyResourcePath()));
465
601
    d_ptr->m_copyResourcePathAction->setEnabled(false);
466
602
 
 
603
    //d_ptr->m_filterWidget = new qdesigner_internal::FilterWidget(0, qdesigner_internal::FilterWidget::LayoutAlignNone);
 
604
    d_ptr->m_filterWidget = new qdesigner_internal::FilterWidget(d_ptr->m_toolBar);
 
605
    d_ptr->m_toolBar->addWidget(d_ptr->m_filterWidget);
 
606
    connect(d_ptr->m_filterWidget, SIGNAL(filterChanged(QString)), this, SLOT(slotFilterChanged(QString)));
 
607
 
467
608
    d_ptr->m_splitter = new QSplitter;
468
609
    d_ptr->m_splitter->setChildrenCollapsible(false);
469
610
    d_ptr->m_splitter->addWidget(d_ptr->m_treeWidget);
499
640
{
500
641
    if (!d_ptr->m_settingsKey.isEmpty())
501
642
        d_ptr->saveSettings();
502
 
 
503
 
    delete d_ptr;
504
643
}
505
644
 
506
645
bool QtResourceView::event(QEvent *event)
737
876
    settings->setValue(QLatin1String(Geometry), geometry());
738
877
 
739
878
    settings->endGroup();
740
 
 
741
 
    delete d_ptr;
742
879
}
743
880
 
744
881
QString QtResourceViewDialog::selectedResource() const