1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
|
/****************************************************************************
**
** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved.
**
** This file is part of the tools applications of the Qt Toolkit.
**
** This file may be used under the terms of the GNU General Public
** License versions 2.0 or 3.0 as published by the Free Software
** Foundation and appearing in the files LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Alternatively you may (at
** your option) use any later version of the GNU General Public
** License if such license has been publicly approved by Trolltech ASA
** (or its successors, if any) and the KDE Free Qt Foundation. In
** addition, as a special exception, Trolltech gives you certain
** additional rights. These rights are described in the Trolltech GPL
** Exception version 1.2, which can be found at
** http://www.trolltech.com/products/qt/gplexception/ and in the file
** GPL_EXCEPTION.txt in this package.
**
** Please review the following information to ensure GNU General
** Public Licensing requirements will be met:
** http://trolltech.com/products/qt/licenses/licensing/opensource/. If
** you are unsure which license is appropriate for your use, please
** review the following information:
** http://trolltech.com/products/qt/licenses/licensing/licensingoverview
** or contact the sales department at sales@trolltech.com.
**
** In addition, as a special exception, Trolltech, as the sole
** copyright holder for Qt Designer, grants users of the Qt/Eclipse
** Integration plug-in the right for the Qt/Eclipse Integration to
** link to functionality provided by Qt Designer and its related
** libraries.
**
** This file is provided "AS IS" with NO WARRANTY OF ANY KIND,
** INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE. Trolltech reserves all rights not expressly
** granted herein.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/
#include "uic.h"
#include "ui4.h"
#include "driver.h"
#include "option.h"
#include "treewalker.h"
#include "validator.h"
#ifdef QT_UIC_CPP_GENERATOR
#include "cppwriteincludes.h"
#include "cppwritedeclaration.h"
#endif
#ifdef QT_UIC_JAVA_GENERATOR
#include "javawriteincludes.h"
#include "javawritedeclaration.h"
#endif
#ifdef QT_UIC_PERL_GENERATOR
#include "plwritedeclaration.h"
#endif
#include <QtXml/QDomDocument>
#include <QtCore/QFileInfo>
#include <QtCore/QRegExp>
#include <QtCore/QTextStream>
#include <QtCore/QDateTime>
#if defined Q_WS_WIN
#include <qt_windows.h>
#endif
#if defined(QT_BEGIN_NAMESPACE)
QT_BEGIN_NAMESPACE
#endif
Uic::Uic(Driver *d)
: drv(d),
out(d->output()),
opt(d->option()),
info(d),
externalPix(true)
{
}
Uic::~Uic()
{
}
bool Uic::printDependencies()
{
QString fileName = opt.inputFile;
QFile f;
if (fileName.isEmpty())
f.open(stdin, QIODevice::ReadOnly);
else {
f.setFileName(fileName);
if (!f.open(QIODevice::ReadOnly))
return false;
}
QDomDocument doc; // ### generalize. share more code with the other tools!
if (!doc.setContent(&f))
return false;
QDomElement root = doc.firstChildElement();
DomUI *ui = new DomUI();
ui->read(root);
double version = ui->attributeVersion().toDouble();
if (version < 4.0) {
delete ui;
fprintf(stderr, "uic: File generated with too old version of Qt Designer\n");
return false;
}
if (DomIncludes *includes = ui->elementIncludes()) {
foreach (DomInclude *incl, includes->elementInclude()) {
QString file = incl->text();
if (file.isEmpty())
continue;
fprintf(stdout, "%s\n", file.toLocal8Bit().constData());
}
}
if (DomCustomWidgets *customWidgets = ui->elementCustomWidgets()) {
foreach (DomCustomWidget *customWidget, customWidgets->elementCustomWidget()) {
if (DomHeader *header = customWidget->elementHeader()) {
QString file = header->text();
if (file.isEmpty())
continue;
fprintf(stdout, "%s\n", file.toLocal8Bit().constData());
}
}
}
delete ui;
return true;
}
void Uic::writeCopyrightHeader(DomUI *ui)
{
QString comment = ui->elementComment();
#ifdef QT_UIC_PERL_GENERATOR
if (comment.size())
out << "\n=begin\n" << comment << "\n=cut\n\n";
out << "\n=begin\n\n";
out << "** Form generated from reading ui file '" << QFileInfo(opt.inputFile).fileName() << "'\n";
out << "**\n";
out << "** Created: " << QDateTime::currentDateTime().toString() << "\n";
out << "** " << QString("by: Qt User Interface Compiler version %1\n").arg(QT_VERSION_STR);
out << "**\n";
out << "** WARNING! All changes made in this file will be lost when recompiling ui file!\n";
out << "\n=cut\n\n";
#else
if (comment.size())
out << "/*\n" << comment << "\n*/\n\n";
out << "/********************************************************************************\n";
out << "** Form generated from reading ui file '" << QFileInfo(opt.inputFile).fileName() << "'\n";
out << "**\n";
out << "** Created: " << QDateTime::currentDateTime().toString() << "\n";
out << "** " << QString::fromLatin1("by: Qt User Interface Compiler version %1\n").arg(QLatin1String(QT_VERSION_STR));
out << "**\n";
out << "** WARNING! All changes made in this file will be lost when recompiling ui file!\n";
out << "********************************************************************************/\n\n";
#endif
}
bool Uic::write(QIODevice *in)
{
QDomDocument doc;
if (!doc.setContent(in))
return false;
if (option().generator == Option::JavaGenerator) {
// the Java generator ignores header protection
opt.headerProtection = false;
}
QDomElement root = doc.firstChildElement();
DomUI *ui = new DomUI();
ui->read(root);
double version = ui->attributeVersion().toDouble();
if (version < 4.0) {
delete ui;
fprintf(stderr, "uic: File generated with too old version of Qt Designer\n");
return false;
}
QString language = ui->attributeLanguage();
bool rtn = false;
if (option().generator == Option::JavaGenerator) {
#ifdef QT_UIC_JAVA_GENERATOR
if (language.toLower() != QLatin1String("jambi")) {
fprintf(stderr, "uic: File is not a 'jambi' form\n");
return false;
}
rtn = jwrite (ui);
#else
fprintf(stderr, "uic: option to generate java code not compiled in\n");
#endif
} else if (option().generator == Option::PerlGenerator) {
#ifdef QT_UIC_PERL_GENERATOR
rtn = plwrite (ui);
#else
fprintf(stderr, "uic: option to generate perl code not compiled in\n");
#endif
} else {
#ifdef QT_UIC_CPP_GENERATOR
if (!language.isEmpty() && language.toLower() != QLatin1String("c++")) {
fprintf(stderr, "uic: File is not a 'c++' ui file, language=%s\n", qPrintable(language));
return false;
}
rtn = write (ui);
#else
fprintf(stderr, "uic: option to generate cpp code not compiled in\n");
#endif
}
delete ui;
return rtn;
}
#ifdef QT_UIC_CPP_GENERATOR
bool Uic::write(DomUI *ui)
{
using namespace CPP;
if (!ui || !ui->elementWidget())
return false;
if (opt.copyrightHeader)
writeCopyrightHeader(ui);
if (opt.headerProtection) {
writeHeaderProtectionStart();
out << "\n";
}
pixFunction = ui->elementPixmapFunction();
if (pixFunction == QLatin1String("QPixmap::fromMimeSource"))
pixFunction = QLatin1String("qPixmapFromMimeSource");
externalPix = ui->elementImages() == 0;
info.acceptUI(ui);
cWidgetsInfo.acceptUI(ui);
WriteIncludes writeIncludes(this);
writeIncludes.acceptUI(ui);
Validator(this).acceptUI(ui);
WriteDeclaration(this, writeIncludes.scriptsActivated()).acceptUI(ui);
if (opt.headerProtection)
writeHeaderProtectionEnd();
return true;
}
#endif
#ifdef QT_UIC_JAVA_GENERATOR
bool Uic::jwrite(DomUI *ui)
{
using namespace Java;
if (!ui || !ui->elementWidget())
return false;
if (opt.copyrightHeader)
writeCopyrightHeader(ui);
pixFunction = ui->elementPixmapFunction();
if (pixFunction == QLatin1String("QPixmap::fromMimeSource"))
pixFunction = QLatin1String("qPixmapFromMimeSource");
externalPix = ui->elementImages() == 0;
info.acceptUI(ui);
cWidgetsInfo.acceptUI(ui);
WriteIncludes(this).acceptUI(ui);
Validator(this).acceptUI(ui);
WriteDeclaration(this).acceptUI(ui);
return true;
}
#endif
#ifdef QT_UIC_PERL_GENERATOR
bool Uic::plwrite(DomUI *ui)
{
using namespace Perl;
if (!ui || !ui->elementWidget())
return false;
if (opt.copyrightHeader)
writeCopyrightHeader(ui);
pixFunction = ui->elementPixmapFunction();
if (pixFunction == QLatin1String("Qt::Pixmap::fromMimeSource"))
pixFunction = QLatin1String("qPixmapFromMimeSource");
externalPix = ui->elementImages() == 0;
info.acceptUI(ui);
cWidgetsInfo.acceptUI(ui);
// WriteIncludes(this).acceptUI(ui);
Validator(this).acceptUI(ui);
if (option().execCode) {
if (option().useKDE) {
out << "use KDE4;" << endl << endl;
} else {
out << "use QtCore4;" << endl << endl;
}
}
WriteDeclaration(this).acceptUI(ui);
if (option().execCode) {
QString qualifiedClassName = ui->elementClass() + option().postfix;
QString className = qualifiedClassName.mid(0, 1).toUpper() + qualifiedClassName.mid(1);
DomWidget* parentWidget = ui->elementWidget();
QString parentClass = parentWidget->attributeClass();
if (option().useKDE) {
out << "\n\nsub main {" << endl;
out << option().indent << "my $about = KDE::AboutData(\"" << className.toLower() << "\", \"" << className << "\", KDE.ki18n(\"\"), \"0.1\")" << endl;
out << option().indent << "KDE::CmdLineArgs.init(\\@ARGV, $about)" << endl;
out << option().indent << "my $a = KDE::Application" << endl;
parentClass.replace(QRegExp("^Q"), "Qt::");
parentClass.replace(QRegExp("^K"), "KDE::");
out << option().indent << "my $w = " << parentClass << endl;
out << option().indent << option().prefix << className << "setupUi($w)" << endl;
out << option().indent << "$a->topWidget = $w" << endl;
out << option().indent << "$w->show()" << endl;
out << option().indent << "$a->exec()" << endl;
out << "}" << endl;
out << "exit main()" << endl;
} else {
out << "\n\nsub main {" << endl;
out << option().indent << "my $a = Qt::Application(\\@ARGV)" << endl;
parentClass.replace(QRegExp("^Q"), "Qt::");
out << option().indent << "my $w = " << parentClass << endl;
out << option().indent << "my $u = " << option().prefix << className << endl;
out << option().indent << "$w->show()" << endl;
out << option().indent << "$a->exec()" << endl;
out << "}" << endl;
out << "exit main()" << endl;
}
}
return true;
}
#endif
#ifdef QT_UIC_CPP_GENERATOR
void Uic::writeHeaderProtectionStart()
{
QString h = drv->headerFileName();
out << "#ifndef " << h << "\n"
<< "#define " << h << "\n";
}
void Uic::writeHeaderProtectionEnd()
{
QString h = drv->headerFileName();
out << "#endif // " << h << "\n";
}
#endif
bool Uic::isMainWindow(const QString &className) const
{
return customWidgetsInfo()->extends(className, QLatin1String("Q3MainWindow"))
|| customWidgetsInfo()->extends(className, QLatin1String("QMainWindow"));
}
bool Uic::isToolBar(const QString &className) const
{
return customWidgetsInfo()->extends(className, QLatin1String("Q3ToolBar"))
|| customWidgetsInfo()->extends(className, QLatin1String("QToolBar"));
}
bool Uic::isButton(const QString &className) const
{
return customWidgetsInfo()->extends(className, QLatin1String("QRadioButton"))
|| customWidgetsInfo()->extends(className, QLatin1String("QToolButton"))
|| customWidgetsInfo()->extends(className, QLatin1String("QCheckBox"))
|| customWidgetsInfo()->extends(className, QLatin1String("QPushButton"));
}
bool Uic::isContainer(const QString &className) const
{
return customWidgetsInfo()->extends(className, QLatin1String("QStackedWidget"))
|| customWidgetsInfo()->extends(className, QLatin1String("QToolBox"))
|| customWidgetsInfo()->extends(className, QLatin1String("QTabWidget"))
|| customWidgetsInfo()->extends(className, QLatin1String("QScrollArea"))
|| customWidgetsInfo()->extends(className, QLatin1String("QMdiArea"))
|| customWidgetsInfo()->extends(className, QLatin1String("QWizard"));
}
bool Uic::isStatusBar(const QString &className) const
{
return customWidgetsInfo()->extends(className, QLatin1String("QStatusBar"));
}
bool Uic::isMenuBar(const QString &className) const
{
return customWidgetsInfo()->extends(className, QLatin1String("QMenuBar"));
}
bool Uic::isMenu(const QString &className) const
{
return customWidgetsInfo()->extends(className, QLatin1String("QMenu"))
|| customWidgetsInfo()->extends(className, QLatin1String("QPopupMenu"));
}
#if defined(QT_END_NAMESPACE)
QT_END_NAMESPACE
#endif
|