1
/****************************************************************************
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
5
** This file is part of the designer application of the Qt Toolkit.
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.
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.
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.
21
** Contact info@trolltech.com if any conditions of this licensing are
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.
27
****************************************************************************/
29
#include "resourcefile_p.h"
30
#include "findicondialog_p.h"
31
#include "ui_findicondialog.h"
33
#include <QtDesigner/abstractformwindow.h>
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>
43
FindIconDialog::FindIconDialog(QDesignerFormWindowInterface *form, QWidget *parent)
46
ui = new Ui::FindIconDialog;
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()));
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();
70
FindIconDialog::~FindIconDialog()
76
void FindIconDialog::setPaths(const QString &qrcPath, const QString &filePath)
78
if (qrcPath.isEmpty()) {
82
if (filePath.endsWith(QString(QDir::separator()))) {
85
QFileInfo fi(filePath);
86
fileDir = fi.absolutePath();
87
fileName = fi.fileName();
89
ui->m_file_dir_input->setText(fileDir);
90
m_icon_file_name = fileName;
92
activateBox(ResourceBox);
93
int idx = ui->m_resource_combo->findText(m_form->absoluteDir().relativeFilePath(qrcPath));
95
ui->m_resource_combo->setCurrentIndex(idx);
96
m_icon_file_name = filePath;
103
void FindIconDialog::resourceSelected(const QModelIndex &index)
105
m_icon_file_name.clear();
107
ResourceModel *model = qobject_cast<ResourceModel*>(ui->m_resource_tree->model());
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;
115
m_icon_file_name.clear();
121
void FindIconDialog::updateBoxes()
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);
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);
140
foreach (QString file, file_list) {
141
QListWidgetItem *item = new QListWidgetItem(ui->m_file_image_list);
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);
150
if (ui->m_file_image_list->currentItem() == 0)
151
m_icon_file_name.clear();
153
int idx = ui->m_resource_combo->currentIndex();
155
QString qrc_file = m_form->absoluteDir().absoluteFilePath(ui->m_resource_combo->itemText(idx));
156
ResourceFile rf(qrc_file);
158
QAbstractItemModel *old_model = ui->m_resource_tree->model();
159
ui->m_resource_tree->setModel(0);
161
ResourceModel *new_model = new ResourceModel(rf);
162
ui->m_resource_tree->setModel(new_model);
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);
172
m_icon_file_name.clear();
175
connect(ui->m_resource_tree->selectionModel(),
176
SIGNAL(currentChanged(QModelIndex,QModelIndex)),
177
this, SLOT(resourceSelected(QModelIndex)));
184
void FindIconDialog::imageFileSelected(QListWidgetItem *item)
186
m_icon_file_name.clear();
188
m_icon_file_name = item->text();
193
void FindIconDialog::updateButtons()
195
ui->m_ok_button->setEnabled(!m_icon_file_name.isEmpty());
198
void FindIconDialog::setActiveBox()
200
activateBox(sender() == ui->m_specify_file_input ? FileBox : ResourceBox);
203
void FindIconDialog::activateBox(InputBox box)
205
ui->m_specify_file_input->setChecked(box == FileBox);
206
ui->m_specify_file_box->setEnabled(box == FileBox);
208
ui->m_specify_resource_box->setEnabled(box == ResourceBox);
209
ui->m_specify_resource_input->setChecked(box == ResourceBox);
214
FindIconDialog::InputBox FindIconDialog::activeBox() const
216
if (ui->m_specify_file_input->isChecked())
221
QString FindIconDialog::qrcPath() const
223
if (activeBox() == FileBox)
225
return m_form->absoluteDir().absoluteFilePath(ui->m_resource_combo->currentText());
228
QString FindIconDialog::filePath() const
230
if (activeBox() == FileBox) {
231
return QDir::cleanPath(ui->m_file_dir_input->text() + QDir::separator() + m_icon_file_name);
233
return QDir::cleanPath(m_icon_file_name);
237
void FindIconDialog::browseFileDir()
239
QString dir = QFileDialog::getExistingDirectory();
241
ui->m_file_dir_input->setText(dir);