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

« back to all changes in this revision

Viewing changes to tools/designer/src/designer/newform.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 "newform.h"
 
30
#include "qdesigner.h"
 
31
#include "qdesigner_workbench.h"
 
32
#include "qdesigner_actions.h"
 
33
#include "qdesigner_formwindow.h"
 
34
#include "qdesigner_settings.h"
 
35
 
 
36
#include <qdesigner_formbuilder_p.h>
 
37
#include <sheet_delegate_p.h>
 
38
 
 
39
#include <QtDesigner/abstractformwindow.h>
 
40
 
 
41
#include <QtCore/QDir>
 
42
#include <QtCore/QFile>
 
43
#include <QtCore/QFileInfo>
 
44
#include <QtGui/QHeaderView>
 
45
#include <QtGui/QPainter>
 
46
 
 
47
#include <QtCore/qdebug.h>
 
48
 
 
49
enum NewForm_CustomRole
 
50
{
 
51
    TemplateNameRole = Qt::UserRole + 100
 
52
};
 
53
 
 
54
NewForm::NewForm(QDesignerWorkbench *workbench, QWidget *parentWidget)
 
55
    : QDialog(parentWidget,
 
56
#ifdef Q_WS_MAC
 
57
            Qt::Tool |
 
58
#endif
 
59
            Qt::WindowTitleHint | Qt::WindowSystemMenuHint),
 
60
      m_workbench(workbench)
 
61
{
 
62
    ui.setupUi(this);
 
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());
 
68
 
 
69
    loadFrom(QLatin1String(":/trolltech/designer/templates/forms"), true);
 
70
 
 
71
    QDesignerSettings settings;
 
72
    foreach(QString path, settings.formTemplatePaths())
 
73
        loadFrom(path, false);
 
74
}
 
75
 
 
76
NewForm::~NewForm()
 
77
{
 
78
    QDesignerSettings().setShowNewFormOnStartup(ui.chkShowOnStartup->isChecked());
 
79
}
 
80
 
 
81
void NewForm::on_treeWidget_currentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *)
 
82
{
 
83
    if (current && current->parent()) {
 
84
        QIcon icon = formPreviewIcon(current->data(0, TemplateNameRole).toString());
 
85
        if (icon.isNull()) {
 
86
            ui.createButton->setEnabled(false);
 
87
            ui.lblPreview->setText(tr("Error loading form"));
 
88
        } else {
 
89
            ui.createButton->setEnabled(true);
 
90
            ui.createButton->setDefault(true);
 
91
            ui.lblPreview->setPixmap(icon.pixmap(QSize(256, 256)));
 
92
        }
 
93
    } else {
 
94
        ui.createButton->setEnabled(false);
 
95
        ui.lblPreview->setText(tr("Choose a template for a preview"));
 
96
    }
 
97
}
 
98
 
 
99
void NewForm::on_treeWidget_itemActivated(QTreeWidgetItem *item)
 
100
{
 
101
    if (item->data(0, TemplateNameRole).isValid())
 
102
        ui.createButton->animateClick(0);
 
103
}
 
104
 
 
105
void NewForm::on_createButton_clicked()
 
106
{
 
107
    if (QTreeWidgetItem *item = ui.treeWidget->currentItem()) {
 
108
        close();
 
109
 
 
110
        int maxUntitled = 0;
 
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)
 
119
                    ++maxUntitled;
 
120
                if (rx.numCaptures() > 1)
 
121
                    maxUntitled = qMax(rx.cap(2).toInt(), maxUntitled);
 
122
            }
 
123
        }
 
124
 
 
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);
 
131
                f.close();
 
132
            } else {
 
133
                editor->setContents(QString());
 
134
            }
 
135
 
 
136
            if (QWidget *container = editor->mainContainer())
 
137
                formWindow->resize(container->size());
 
138
        }
 
139
        QString newTitle = QString::fromUtf8("Untitled");
 
140
        if (maxUntitled)
 
141
            newTitle += QString::fromUtf8(" ") + QString::number(maxUntitled + 1);
 
142
 
 
143
        newTitle.append(QLatin1String("[*]"));
 
144
        formWindow->setWindowTitle(newTitle);
 
145
        formWindow->show();
 
146
    }
 
147
}
 
148
 
 
149
void NewForm::on_closeButton_clicked()
 
150
{
 
151
    close();
 
152
}
 
153
 
 
154
QDesignerWorkbench *NewForm::workbench() const
 
155
{
 
156
    return m_workbench;
 
157
}
 
158
 
 
159
QIcon NewForm::formPreviewIcon(const QString &fileName)
 
160
{
 
161
    QIcon result;
 
162
 
 
163
    QFile f(fileName);
 
164
    if (f.open(QFile::ReadOnly)) {
 
165
        QDesignerFormBuilder formBuilder(workbench()->core());
 
166
 
 
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);
 
172
            widget->show();
 
173
            f.close();
 
174
 
 
175
            QList<QLayout*> layouts = qFindChildren<QLayout*>(widget);
 
176
            foreach (QLayout *layout, layouts)
 
177
                layout->activate();
 
178
 
 
179
            widget->ensurePolished();
 
180
            qDesigner->processEvents();
 
181
 
 
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);
 
186
        }
 
187
 
 
188
        fake->deleteLater();
 
189
    }
 
190
 
 
191
    return result;
 
192
}
 
193
 
 
194
void NewForm::loadFrom(const QString &path, bool resourceFile)
 
195
{
 
196
    QDir dir(path);
 
197
 
 
198
    if (!dir.exists())
 
199
        return;
 
200
 
 
201
    // Iterate through the directory and add the templates
 
202
    QFileInfoList list = dir.entryInfoList(QStringList() << "*.ui", QDir::Files);
 
203
 
 
204
    if (list.isEmpty())
 
205
        return;
 
206
 
 
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);
 
213
    if (index != -1) {
 
214
        // try to find a second slash, just to be a bit better.
 
215
        int index2 = visiblePath.lastIndexOf(separator, index - 1);
 
216
        if (index2 != -1)
 
217
            index = index2;
 
218
        visiblePath = visiblePath.mid(index + 1);
 
219
        visiblePath = QDir::convertSeparators(visiblePath);
 
220
    }
 
221
 
 
222
    root->setText(0, visiblePath.replace(QLatin1String("_"), QLatin1String(" ")));
 
223
    root->setToolTip(0, path);
 
224
 
 
225
    foreach(QFileInfo fi, list) {
 
226
        if (!fi.isFile())
 
227
            continue;
 
228
 
 
229
        QTreeWidgetItem *item = new QTreeWidgetItem(root);
 
230
        item->setText(0, fi.baseName().replace(QLatin1String("_"), QLatin1String(" ")));
 
231
        item->setData(0, TemplateNameRole, fi.absoluteFilePath());
 
232
 
 
233
        QTreeWidgetItem *i = ui.treeWidget->currentItem();
 
234
        if (i == 0) {
 
235
            ui.treeWidget->setCurrentItem(item);
 
236
            ui.treeWidget->setItemSelected(item, true);
 
237
        }
 
238
    }
 
239
    ui.treeWidget->setItemExpanded(root, true);
 
240
}
 
241
 
 
242
void NewForm::on_openButton_clicked()
 
243
{
 
244
    hide();
 
245
    if (m_workbench->actionManager()->openForm())
 
246
        close();
 
247
    else
 
248
        show();
 
249
}
 
250
 
 
251
void NewForm::on_treeWidget_itemPressed(QTreeWidgetItem *item)
 
252
{
 
253
    if (item && !item->parent())
 
254
        ui.treeWidget->setItemExpanded(item, !ui.treeWidget->isItemExpanded(item));
 
255
}