source: trunk/tools/designer/src/lib/shared/dialoggui.cpp@ 651

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

trunk: Merged in qt 4.6.2 sources.

File size: 9.5 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the Qt Designer of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
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** 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 have questions regarding the use of this file, please contact
37** Nokia at [email protected].
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
59QT_BEGIN_NAMESPACE
60
61namespace qdesigner_internal {
62
63// Icon provider that reads out the known image formats
64class IconProvider : public QFileIconProvider {
65 Q_DISABLE_COPY(IconProvider)
66
67public:
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
74private:
75 QSet<QString> m_imageFormats;
76};
77
78IconProvider::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
93bool 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
103QImage 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
117QIcon 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
130DialogGui::DialogGui() :
131 m_iconProvider(0)
132{
133}
134
135DialogGui::~DialogGui()
136{
137 delete m_iconProvider;
138}
139
140QFileIconProvider *DialogGui::ensureIconProvider()
141{
142 if (!m_iconProvider)
143 m_iconProvider = new IconProvider;
144 return m_iconProvider;
145}
146
147QMessageBox::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
172QMessageBox::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
183QMessageBox::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
195QString DialogGui::getExistingDirectory(QWidget *parent, const QString &caption, const QString &dir, QFileDialog::Options options)
196{
197 return QFileDialog::getExistingDirectory(parent, caption, dir, options);
198}
199
200QString 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
205QStringList 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
210QString 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
215void 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
223QString 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
245QStringList 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
265QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.