author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Mon, 15 Mar 2010 12:43:09 +0200 | |
branch | RCL_3 |
changeset 6 | dee5afe5301f |
parent 4 | 3b1da2848fc7 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the Qt Designer of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
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. |
|
15 |
** |
|
16 |
** GNU Lesser General Public License Usage |
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 |
** General Public License version 2.1 as published by the Free Software |
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 |
** packaging of this file. Please review the following information to |
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 |
** |
|
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 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
****************************************************************************/ |
|
41 |
||
42 |
#include "dialoggui_p.h" |
|
43 |
||
44 |
#include <QtGui/QFileIconProvider> |
|
45 |
#include <QtGui/QIcon> |
|
46 |
#include <QtGui/QImage> |
|
47 |
#include <QtGui/QImageReader> |
|
48 |
#include <QtGui/QPixmap> |
|
49 |
||
50 |
#include <QtCore/QFileInfo> |
|
51 |
#include <QtCore/QFile> |
|
52 |
#include <QtCore/QSet> |
|
53 |
||
54 |
// QFileDialog on X11 does not provide an image preview. Display icons. |
|
55 |
#ifdef Q_WS_X11 |
|
56 |
# define IMAGE_PREVIEW |
|
57 |
#endif |
|
58 |
||
59 |
QT_BEGIN_NAMESPACE |
|
60 |
||
61 |
namespace qdesigner_internal { |
|
62 |
||
63 |
// Icon provider that reads out the known image formats |
|
64 |
class IconProvider : public QFileIconProvider { |
|
65 |
Q_DISABLE_COPY(IconProvider) |
|
66 |
||
67 |
public: |
|
68 |
IconProvider(); |
|
69 |
virtual QIcon icon (const QFileInfo &info) const; |
|
70 |
||
71 |
inline bool loadCheck(const QFileInfo &info) const; |
|
72 |
QImage loadImage(const QString &fileName) const; |
|
73 |
||
74 |
private: |
|
75 |
QSet<QString> m_imageFormats; |
|
76 |
}; |
|
77 |
||
78 |
IconProvider::IconProvider() |
|
79 |
{ |
|
80 |
// Determine a list of readable extensions (upper and lower case) |
|
81 |
typedef QList<QByteArray> ByteArrayList; |
|
82 |
const ByteArrayList fmts = QImageReader::supportedImageFormats(); |
|
83 |
const ByteArrayList::const_iterator cend = fmts.constEnd(); |
|
84 |
for (ByteArrayList::const_iterator it = fmts.constBegin(); it != cend; ++it) { |
|
85 |
const QString suffix = QString::fromUtf8(it->constData()); |
|
86 |
m_imageFormats.insert(suffix.toLower()); |
|
87 |
m_imageFormats.insert(suffix.toUpper()); |
|
88 |
||
89 |
} |
|
90 |
} |
|
91 |
||
92 |
// Check by extension and type if this appears to be a loadable image |
|
93 |
bool IconProvider::loadCheck(const QFileInfo &info) const |
|
94 |
{ |
|
95 |
if (info.isFile() && info.isReadable()) { |
|
96 |
const QString suffix = info.suffix(); |
|
97 |
if (!suffix.isEmpty()) |
|
98 |
return m_imageFormats.contains(suffix); |
|
99 |
} |
|
100 |
return false; |
|
101 |
} |
|
102 |
||
103 |
QImage IconProvider::loadImage(const QString &fileName) const |
|
104 |
{ |
|
105 |
QFile file(fileName); |
|
106 |
if (file.open(QIODevice::ReadOnly)) { |
|
107 |
QImageReader imgReader(&file); |
|
108 |
if (imgReader.canRead()) { |
|
109 |
QImage image; |
|
110 |
if (imgReader.read(&image)) |
|
111 |
return image; |
|
112 |
} |
|
113 |
} |
|
114 |
return QImage(); |
|
115 |
} |
|
116 |
||
117 |
QIcon IconProvider::icon (const QFileInfo &info) const |
|
118 |
{ |
|
119 |
// Don't get stuck on large images. |
|
120 |
const qint64 maxSize = 131072; |
|
121 |
if (loadCheck(info) && info.size() < maxSize) { |
|
122 |
const QImage image = loadImage(info.absoluteFilePath()); |
|
123 |
if (!image.isNull()) |
|
124 |
return QIcon(QPixmap::fromImage(image, Qt::ThresholdDither|Qt::AutoColor)); |
|
125 |
} |
|
126 |
return QFileIconProvider::icon(info); |
|
127 |
} |
|
128 |
||
129 |
// ---------------- DialogGui |
|
130 |
DialogGui::DialogGui() : |
|
131 |
m_iconProvider(0) |
|
132 |
{ |
|
133 |
} |
|
134 |
||
135 |
DialogGui::~DialogGui() |
|
136 |
{ |
|
137 |
delete m_iconProvider; |
|
138 |
} |
|
139 |
||
140 |
QFileIconProvider *DialogGui::ensureIconProvider() |
|
141 |
{ |
|
142 |
if (!m_iconProvider) |
|
143 |
m_iconProvider = new IconProvider; |
|
144 |
return m_iconProvider; |
|
145 |
} |
|
146 |
||
147 |
QMessageBox::StandardButton |
|
148 |
DialogGui::message(QWidget *parent, Message /*context*/, QMessageBox::Icon icon, |
|
149 |
const QString &title, const QString &text, QMessageBox::StandardButtons buttons, |
|
150 |
QMessageBox::StandardButton defaultButton) |
|
151 |
{ |
|
152 |
QMessageBox::StandardButton rc = QMessageBox::NoButton; |
|
153 |
switch (icon) { |
|
154 |
case QMessageBox::Information: |
|
155 |
rc = QMessageBox::information(parent, title, text, buttons, defaultButton); |
|
156 |
break; |
|
157 |
case QMessageBox::Warning: |
|
158 |
rc = QMessageBox::warning(parent, title, text, buttons, defaultButton); |
|
159 |
break; |
|
160 |
case QMessageBox::Critical: |
|
161 |
rc = QMessageBox::critical(parent, title, text, buttons, defaultButton); |
|
162 |
break; |
|
163 |
case QMessageBox::Question: |
|
164 |
rc = QMessageBox::question(parent, title, text, buttons, defaultButton); |
|
165 |
break; |
|
166 |
case QMessageBox::NoIcon: |
|
167 |
break; |
|
168 |
} |
|
169 |
return rc; |
|
170 |
} |
|
171 |
||
172 |
QMessageBox::StandardButton |
|
173 |
DialogGui::message(QWidget *parent, Message /*context*/, QMessageBox::Icon icon, |
|
174 |
const QString &title, const QString &text, const QString &informativeText, |
|
175 |
QMessageBox::StandardButtons buttons, QMessageBox::StandardButton defaultButton) |
|
176 |
{ |
|
177 |
QMessageBox msgBox(icon, title, text, buttons, parent); |
|
178 |
msgBox.setDefaultButton(defaultButton); |
|
179 |
msgBox.setInformativeText(informativeText); |
|
180 |
return static_cast<QMessageBox::StandardButton>(msgBox.exec()); |
|
181 |
} |
|
182 |
||
183 |
QMessageBox::StandardButton |
|
184 |
DialogGui::message(QWidget *parent, Message /*context*/, QMessageBox::Icon icon, |
|
185 |
const QString &title, const QString &text, const QString &informativeText, const QString &detailedText, |
|
186 |
QMessageBox::StandardButtons buttons, QMessageBox::StandardButton defaultButton) |
|
187 |
{ |
|
188 |
QMessageBox msgBox(icon, title, text, buttons, parent); |
|
189 |
msgBox.setDefaultButton(defaultButton); |
|
190 |
msgBox.setInformativeText(informativeText); |
|
191 |
msgBox.setDetailedText(detailedText); |
|
192 |
return static_cast<QMessageBox::StandardButton>(msgBox.exec()); |
|
193 |
} |
|
194 |
||
195 |
QString DialogGui::getExistingDirectory(QWidget *parent, const QString &caption, const QString &dir, QFileDialog::Options options) |
|
196 |
{ |
|
197 |
return QFileDialog::getExistingDirectory(parent, caption, dir, options); |
|
198 |
} |
|
199 |
||
200 |
QString DialogGui::getOpenFileName(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, QFileDialog::Options options) |
|
201 |
{ |
|
202 |
return QFileDialog::getOpenFileName(parent, caption, dir, filter, selectedFilter, options); |
|
203 |
} |
|
204 |
||
205 |
QStringList DialogGui::getOpenFileNames(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, QFileDialog::Options options) |
|
206 |
{ |
|
207 |
return QFileDialog::getOpenFileNames(parent, caption, dir, filter, selectedFilter, options); |
|
208 |
} |
|
209 |
||
210 |
QString DialogGui::getSaveFileName(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, QFileDialog::Options options) |
|
211 |
{ |
|
212 |
return QFileDialog::getSaveFileName(parent, caption, dir, filter, selectedFilter, options); |
|
213 |
} |
|
214 |
||
215 |
void DialogGui::initializeImageFileDialog(QFileDialog &fileDialog, QFileDialog::Options options, QFileDialog::FileMode fm) |
|
216 |
{ |
|
217 |
fileDialog.setConfirmOverwrite( !(options & QFileDialog::DontConfirmOverwrite) ); |
|
218 |
fileDialog.setResolveSymlinks( !(options & QFileDialog::DontResolveSymlinks) ); |
|
219 |
fileDialog.setIconProvider(ensureIconProvider()); |
|
220 |
fileDialog.setFileMode(fm); |
|
221 |
} |
|
222 |
||
223 |
QString DialogGui::getOpenImageFileName(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, QFileDialog::Options options ) |
|
224 |
{ |
|
225 |
||
226 |
#ifdef IMAGE_PREVIEW |
|
227 |
QFileDialog fileDialog(parent, caption, dir, filter); |
|
228 |
initializeImageFileDialog(fileDialog, options, QFileDialog::ExistingFile); |
|
229 |
if (fileDialog.exec() != QDialog::Accepted) |
|
230 |
return QString(); |
|
231 |
||
232 |
const QStringList selectedFiles = fileDialog.selectedFiles(); |
|
233 |
if (selectedFiles.empty()) |
|
234 |
return QString(); |
|
235 |
||
236 |
if (selectedFilter) |
|
237 |
*selectedFilter = fileDialog.selectedFilter(); |
|
238 |
||
239 |
return selectedFiles.front(); |
|
240 |
#else |
|
241 |
return getOpenFileName(parent, caption, dir, filter, selectedFilter, options); |
|
242 |
#endif |
|
243 |
} |
|
244 |
||
245 |
QStringList DialogGui::getOpenImageFileNames(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, QFileDialog::Options options ) |
|
246 |
{ |
|
247 |
#ifdef IMAGE_PREVIEW |
|
248 |
QFileDialog fileDialog(parent, caption, dir, filter); |
|
249 |
initializeImageFileDialog(fileDialog, options, QFileDialog::ExistingFiles); |
|
250 |
if (fileDialog.exec() != QDialog::Accepted) |
|
251 |
return QStringList(); |
|
252 |
||
253 |
const QStringList selectedFiles = fileDialog.selectedFiles(); |
|
254 |
if (!selectedFiles.empty() && selectedFilter) |
|
255 |
*selectedFilter = fileDialog.selectedFilter(); |
|
256 |
||
257 |
return selectedFiles; |
|
258 |
#else |
|
259 |
return getOpenFileNames(parent, caption, dir, filter, selectedFilter, options); |
|
260 |
#endif |
|
261 |
} |
|
262 |
||
263 |
} |
|
264 |
||
265 |
QT_END_NAMESPACE |