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
****************************************************************************/
30
#include "qdesigner.h"
31
#include "qdesigner_workbench.h"
32
#include "qdesigner_actions.h"
33
#include "qdesigner_formwindow.h"
34
#include "qdesigner_settings.h"
36
#include <qdesigner_formbuilder_p.h>
37
#include <sheet_delegate_p.h>
39
#include <QtDesigner/abstractformwindow.h>
41
#include <QtCore/QDir>
42
#include <QtCore/QFile>
43
#include <QtCore/QFileInfo>
44
#include <QtGui/QHeaderView>
45
#include <QtGui/QPainter>
47
#include <QtCore/qdebug.h>
49
enum NewForm_CustomRole
51
TemplateNameRole = Qt::UserRole + 100
54
NewForm::NewForm(QDesignerWorkbench *workbench, QWidget *parentWidget)
55
: QDialog(parentWidget,
59
Qt::WindowTitleHint | Qt::WindowSystemMenuHint),
60
m_workbench(workbench)
63
ui.treeWidget->setItemDelegate(new SheetDelegate(ui.treeWidget, this));
64
ui.treeWidget->header()->hide();
65
ui.treeWidget->header()->setStretchLastSection(true);
66
ui.lblPreview->setBackgroundRole(QPalette::Base);
67
ui.chkShowOnStartup->setChecked(QDesignerSettings().showNewFormOnStartup());
69
loadFrom(QLatin1String(":/trolltech/designer/templates/forms"), true);
71
QDesignerSettings settings;
72
foreach(QString path, settings.formTemplatePaths())
73
loadFrom(path, false);
78
QDesignerSettings().setShowNewFormOnStartup(ui.chkShowOnStartup->isChecked());
81
void NewForm::on_treeWidget_currentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *)
83
if (current && current->parent()) {
84
QIcon icon = formPreviewIcon(current->data(0, TemplateNameRole).toString());
86
ui.createButton->setEnabled(false);
87
ui.lblPreview->setText(tr("Error loading form"));
89
ui.createButton->setEnabled(true);
90
ui.createButton->setDefault(true);
91
ui.lblPreview->setPixmap(icon.pixmap(QSize(256, 256)));
94
ui.createButton->setEnabled(false);
95
ui.lblPreview->setText(tr("Choose a template for a preview"));
99
void NewForm::on_treeWidget_itemActivated(QTreeWidgetItem *item)
101
if (item->data(0, TemplateNameRole).isValid())
102
ui.createButton->animateClick(0);
105
void NewForm::on_createButton_clicked()
107
if (QTreeWidgetItem *item = ui.treeWidget->currentItem()) {
111
int totalWindows = m_workbench->formWindowCount();
112
// This will cause some problems with i18n, but for now I need the string to be "static"
113
QRegExp rx(QLatin1String("Untitled( (\\d+))*"));
114
for (int i = 0; i < totalWindows; ++i) {
115
QString title = m_workbench->formWindow(i)->windowTitle();
116
title = title.replace(QLatin1String("[*]"), QLatin1String(""));
117
if (rx.exactMatch(title)) {
118
if (maxUntitled == 0)
120
if (rx.numCaptures() > 1)
121
maxUntitled = qMax(rx.cap(2).toInt(), maxUntitled);
125
QDesignerFormWindow *formWindow = workbench()->createFormWindow();
126
if (QDesignerFormWindowInterface *editor = formWindow->editor()) {
127
QString formTemplateName = item->data(0, TemplateNameRole).toString();
128
QFile f(formTemplateName);
129
if (f.open(QFile::ReadOnly)) {
130
editor->setContents(&f);
133
editor->setContents(QString());
136
if (QWidget *container = editor->mainContainer())
137
formWindow->resize(container->size());
139
QString newTitle = QString::fromUtf8("Untitled");
141
newTitle += QString::fromUtf8(" ") + QString::number(maxUntitled + 1);
143
newTitle.append(QLatin1String("[*]"));
144
formWindow->setWindowTitle(newTitle);
149
void NewForm::on_closeButton_clicked()
154
QDesignerWorkbench *NewForm::workbench() const
159
QIcon NewForm::formPreviewIcon(const QString &fileName)
164
if (f.open(QFile::ReadOnly)) {
165
QDesignerFormBuilder formBuilder(workbench()->core());
167
QWidget *fake = new QWidget(0);
168
if (QWidget *widget = formBuilder.load(&f, fake)) {
169
QSize size = widget->size();
170
widget->setParent(fake, 0);
171
widget->resize(size);
175
QList<QLayout*> layouts = qFindChildren<QLayout*>(widget);
176
foreach (QLayout *layout, layouts)
179
widget->ensurePolished();
180
qDesigner->processEvents();
182
QPixmap pix = QPixmap::grabWidget(widget);
183
QImage image = pix.toImage();
184
image = image.scaled(256, 256, Qt::KeepAspectRatio, Qt::SmoothTransformation);
185
result = QPixmap::fromImage(image);
194
void NewForm::loadFrom(const QString &path, bool resourceFile)
201
// Iterate through the directory and add the templates
202
QFileInfoList list = dir.entryInfoList(QStringList() << "*.ui", QDir::Files);
207
QChar separator = resourceFile ? QChar(QLatin1Char('/'))
208
: QChar(QDir::separator());
209
QTreeWidgetItem *root = new QTreeWidgetItem(ui.treeWidget);
210
// Try to get something that is easy to read.
211
QString visiblePath = path;
212
int index = visiblePath.lastIndexOf(separator);
214
// try to find a second slash, just to be a bit better.
215
int index2 = visiblePath.lastIndexOf(separator, index - 1);
218
visiblePath = visiblePath.mid(index + 1);
219
visiblePath = QDir::convertSeparators(visiblePath);
222
root->setText(0, visiblePath.replace(QLatin1String("_"), QLatin1String(" ")));
223
root->setToolTip(0, path);
225
foreach(QFileInfo fi, list) {
229
QTreeWidgetItem *item = new QTreeWidgetItem(root);
230
item->setText(0, fi.baseName().replace(QLatin1String("_"), QLatin1String(" ")));
231
item->setData(0, TemplateNameRole, fi.absoluteFilePath());
233
QTreeWidgetItem *i = ui.treeWidget->currentItem();
235
ui.treeWidget->setCurrentItem(item);
236
ui.treeWidget->setItemSelected(item, true);
239
ui.treeWidget->setItemExpanded(root, true);
242
void NewForm::on_openButton_clicked()
245
if (m_workbench->actionManager()->openForm())
251
void NewForm::on_treeWidget_itemPressed(QTreeWidgetItem *item)
253
if (item && !item->parent())
254
ui.treeWidget->setItemExpanded(item, !ui.treeWidget->isItemExpanded(item));