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

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
 
4
**
 
5
** This file is part of the designer application of the Qt Toolkit.
 
6
**
 
7
** This file may be distributed under the terms of the Q Public License
 
8
** as defined by Trolltech AS of Norway and appearing in the file
 
9
** LICENSE.QPL included in the packaging of this file.
 
10
**
 
11
** This file may be distributed and/or modified under the terms of the
 
12
** GNU General Public License version 2 as published by the Free Software
 
13
** Foundation and appearing in the file LICENSE.GPL included in the
 
14
** packaging of this file.
 
15
**
 
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
 
17
**   information about Qt Commercial License Agreements.
 
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
 
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
 
20
**
 
21
** Contact info@trolltech.com if any conditions of this licensing are
 
22
** not clear to you.
 
23
**
 
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 
26
**
 
27
****************************************************************************/
 
28
 
 
29
#include "resourcefile_p.h"
 
30
#include "findicondialog_p.h"
 
31
#include "ui_findicondialog.h"
 
32
 
 
33
#include <QtDesigner/abstractformwindow.h>
 
34
 
 
35
#include <QtCore/QFileInfo>
 
36
#include <QtCore/QDir>
 
37
#include <QtCore/qdebug.h>
 
38
#include <QtGui/QFileDialog>
 
39
#include <QtGui/QHeaderView>
 
40
#include <QtGui/QImageReader>
 
41
 
 
42
 
 
43
FindIconDialog::FindIconDialog(QDesignerFormWindowInterface *form, QWidget *parent)
 
44
    : QDialog(parent)
 
45
{
 
46
    ui = new Ui::FindIconDialog;
 
47
    ui->setupUi(this);
 
48
 
 
49
    connect(ui->m_file_dir_input, SIGNAL(textChanged(QString)), this, SLOT(updateBoxes()));
 
50
    connect(ui->m_ok_button, SIGNAL(clicked()), this, SLOT(accept()));
 
51
    connect(ui->m_cancel_button, SIGNAL(clicked()), this, SLOT(reject()));
 
52
    connect(ui->m_file_image_list, SIGNAL(currentItemChanged(QListWidgetItem*,QListWidgetItem*)),
 
53
                this, SLOT(imageFileSelected(QListWidgetItem*)));
 
54
    connect(ui->m_file_dir_browse, SIGNAL(clicked()), this, SLOT(browseFileDir()));
 
55
    connect(ui->m_specify_file_input, SIGNAL(clicked()), this, SLOT(setActiveBox()));
 
56
    connect(ui->m_specify_resource_input, SIGNAL(clicked()), this, SLOT(setActiveBox()));
 
57
    connect(ui->m_resource_combo, SIGNAL(activated(int)), this, SLOT(updateBoxes()));
 
58
 
 
59
    m_form = form;
 
60
    QStringList res_list = m_form->resourceFiles();
 
61
    QStringList rel_res_list;
 
62
    foreach (QString res, res_list)
 
63
        rel_res_list.append(m_form->absoluteDir().relativeFilePath(res));
 
64
    ui->m_resource_combo->addItems(rel_res_list);
 
65
    ui->m_resource_tree->header()->hide();
 
66
 
 
67
    updateButtons();
 
68
}
 
69
 
 
70
FindIconDialog::~FindIconDialog()
 
71
{
 
72
    delete ui;
 
73
    ui = 0;
 
74
}
 
75
 
 
76
void FindIconDialog::setPaths(const QString &qrcPath, const QString &filePath)
 
77
{
 
78
    if (qrcPath.isEmpty()) {
 
79
        activateBox(FileBox);
 
80
        QString fileDir;
 
81
        QString fileName;
 
82
        if (filePath.endsWith(QString(QDir::separator()))) {
 
83
            fileDir = filePath;
 
84
        } else {
 
85
            QFileInfo fi(filePath);
 
86
            fileDir = fi.absolutePath();
 
87
            fileName = fi.fileName();
 
88
        }
 
89
        ui->m_file_dir_input->setText(fileDir);
 
90
        m_icon_file_name = fileName;
 
91
    } else {
 
92
        activateBox(ResourceBox);
 
93
        int idx = ui->m_resource_combo->findText(m_form->absoluteDir().relativeFilePath(qrcPath));
 
94
        if (idx != -1) {
 
95
            ui->m_resource_combo->setCurrentIndex(idx);
 
96
            m_icon_file_name = filePath;
 
97
        }
 
98
    }
 
99
 
 
100
    updateBoxes();
 
101
}
 
102
 
 
103
void FindIconDialog::resourceSelected(const QModelIndex &index)
 
104
{
 
105
    m_icon_file_name.clear();
 
106
 
 
107
    ResourceModel *model = qobject_cast<ResourceModel*>(ui->m_resource_tree->model());
 
108
    if (model != 0) {
 
109
        QString prefix, file;
 
110
        model->getItem(index, prefix, file);
 
111
        if (!file.isEmpty()) {
 
112
            if (model->iconFileExtension(file))
 
113
                m_icon_file_name = QLatin1String(":") + prefix + QLatin1String("/") + file;
 
114
            else
 
115
                m_icon_file_name.clear();
 
116
        }
 
117
    }
 
118
    updateButtons();
 
119
}
 
120
 
 
121
void FindIconDialog::updateBoxes()
 
122
{
 
123
    if (activeBox() == FileBox) {
 
124
        ui->m_file_image_list->clear();
 
125
        QString dir_path = ui->m_file_dir_input->text();
 
126
        if (dir_path.isEmpty()) {
 
127
            dir_path = m_form->absoluteDir().absolutePath();
 
128
            bool blocked = ui->m_file_dir_input->blockSignals(true);
 
129
            ui->m_file_dir_input->setText(dir_path);
 
130
            ui->m_file_dir_input->blockSignals(blocked);
 
131
        }
 
132
        QDir dir(dir_path);
 
133
        if (dir.exists()) {
 
134
            QList<QByteArray> _extension_list = QImageReader::supportedImageFormats();
 
135
            QStringList extension_list;
 
136
            foreach (const QByteArray &ext, _extension_list)
 
137
                extension_list.append(QLatin1String("*.") + QString::fromAscii(ext));
 
138
            QStringList file_list = dir.entryList(extension_list);
 
139
 
 
140
            foreach (QString file, file_list) {
 
141
                QListWidgetItem *item = new QListWidgetItem(ui->m_file_image_list);
 
142
                item->setText(file);
 
143
                item->setIcon(QIcon(dir.filePath(file)));
 
144
                if (item->text() == m_icon_file_name) {
 
145
                    ui->m_file_image_list->setItemSelected(item, true);
 
146
                    ui->m_file_image_list->setCurrentItem(item);
 
147
                }
 
148
            }
 
149
        }
 
150
        if (ui->m_file_image_list->currentItem() == 0)
 
151
            m_icon_file_name.clear();
 
152
    } else {
 
153
        int idx = ui->m_resource_combo->currentIndex();
 
154
        if (idx != -1) {
 
155
            QString qrc_file = m_form->absoluteDir().absoluteFilePath(ui->m_resource_combo->itemText(idx));
 
156
            ResourceFile rf(qrc_file);
 
157
            rf.load();
 
158
            QAbstractItemModel *old_model = ui->m_resource_tree->model();
 
159
            ui->m_resource_tree->setModel(0);
 
160
            delete old_model;
 
161
            ResourceModel *new_model = new ResourceModel(rf);
 
162
            ui->m_resource_tree->setModel(new_model);
 
163
 
 
164
            if (m_icon_file_name.startsWith(QLatin1String(":"))) {
 
165
                QString prefix, file;
 
166
                rf.split(m_icon_file_name, &prefix, &file);
 
167
                QModelIndex file_index = new_model->getIndex(prefix, file);
 
168
                QModelIndex prefix_index = new_model->prefixIndex(file_index);
 
169
                ui->m_resource_tree->setExpanded(prefix_index, true);
 
170
                ui->m_resource_tree->setCurrentIndex(file_index);
 
171
            } else {
 
172
                m_icon_file_name.clear();
 
173
            }
 
174
 
 
175
            connect(ui->m_resource_tree->selectionModel(),
 
176
                        SIGNAL(currentChanged(QModelIndex,QModelIndex)),
 
177
                        this, SLOT(resourceSelected(QModelIndex)));
 
178
        }
 
179
    }
 
180
 
 
181
    updateButtons();
 
182
}
 
183
 
 
184
void FindIconDialog::imageFileSelected(QListWidgetItem *item)
 
185
{
 
186
    m_icon_file_name.clear();
 
187
    if (item != 0)
 
188
        m_icon_file_name = item->text();
 
189
 
 
190
    updateButtons();
 
191
}
 
192
 
 
193
void FindIconDialog::updateButtons()
 
194
{
 
195
    ui->m_ok_button->setEnabled(!m_icon_file_name.isEmpty());
 
196
}
 
197
 
 
198
void FindIconDialog::setActiveBox()
 
199
{
 
200
    activateBox(sender() == ui->m_specify_file_input ? FileBox : ResourceBox);
 
201
}
 
202
 
 
203
void FindIconDialog::activateBox(InputBox box)
 
204
{
 
205
    ui->m_specify_file_input->setChecked(box == FileBox);
 
206
    ui->m_specify_file_box->setEnabled(box == FileBox);
 
207
 
 
208
    ui->m_specify_resource_box->setEnabled(box == ResourceBox);
 
209
    ui->m_specify_resource_input->setChecked(box == ResourceBox);
 
210
 
 
211
    updateBoxes();
 
212
}
 
213
 
 
214
FindIconDialog::InputBox FindIconDialog::activeBox() const
 
215
{
 
216
    if (ui->m_specify_file_input->isChecked())
 
217
        return FileBox;
 
218
    return ResourceBox;
 
219
}
 
220
 
 
221
QString FindIconDialog::qrcPath() const
 
222
{
 
223
    if (activeBox() == FileBox)
 
224
        return QString();
 
225
    return m_form->absoluteDir().absoluteFilePath(ui->m_resource_combo->currentText());
 
226
}
 
227
 
 
228
QString FindIconDialog::filePath() const
 
229
{
 
230
    if (activeBox() == FileBox) {
 
231
        return QDir::cleanPath(ui->m_file_dir_input->text() + QDir::separator() + m_icon_file_name);
 
232
    } else {
 
233
        return QDir::cleanPath(m_icon_file_name);
 
234
    }
 
235
}
 
236
 
 
237
void FindIconDialog::browseFileDir()
 
238
{
 
239
    QString dir = QFileDialog::getExistingDirectory();
 
240
    if (!dir.isEmpty())
 
241
        ui->m_file_dir_input->setText(dir);
 
242
}
 
243