QtBase: examples/widgets/richtext code style

Change-Id: Ie0c27464e0fdb8f26caf0d8f27043da4ec45f41f
Reviewed-by: hjk <qthjk@ovi.com>
This commit is contained in:
David Schulz 2012-11-23 13:36:40 +01:00 committed by The Qt Project
parent cf1e315e28
commit d5ec06be63
15 changed files with 90 additions and 87 deletions

View File

@ -38,7 +38,7 @@
** **
****************************************************************************/ ****************************************************************************/
#include <QtWidgets> #include <QApplication>
#include "mainwindow.h" #include "mainwindow.h"

View File

@ -38,10 +38,10 @@
** **
****************************************************************************/ ****************************************************************************/
#include <QtWidgets>
#include "mainwindow.h" #include "mainwindow.h"
#include <QtWidgets>
//! [0] //! [0]
MainWindow::MainWindow() MainWindow::MainWindow()
{ {
@ -158,8 +158,7 @@ void MainWindow::insertCalendar()
QTextTableCell cell = table->cellAt(0, weekDay-1); QTextTableCell cell = table->cellAt(0, weekDay-1);
//! [11] //! [12] //! [11] //! [12]
QTextCursor cellCursor = cell.firstCursorPosition(); QTextCursor cellCursor = cell.firstCursorPosition();
cellCursor.insertText(QString("%1").arg(QDate::longDayName(weekDay)), cellCursor.insertText(QString("%1").arg(QDate::longDayName(weekDay)), boldFormat);
boldFormat);
} }
//! [12] //! [12]

View File

@ -70,4 +70,4 @@ private:
}; };
//! [0] //! [0]
#endif #endif // MAINWINDOW_H

View File

@ -44,7 +44,6 @@
#include <QDialog> #include <QDialog>
#include <QList> #include <QList>
#include <QPair> #include <QPair>
#include <QString>
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
class QCheckBox; class QCheckBox;
@ -87,4 +86,4 @@ private:
}; };
//! [0] //! [0]
#endif #endif // DETAILSDIALOG_H

View File

@ -38,7 +38,7 @@
** **
****************************************************************************/ ****************************************************************************/
#include <QtWidgets> #include <QApplication>
#include "mainwindow.h" #include "mainwindow.h"

View File

@ -225,9 +225,10 @@ void MainWindow::openDialog()
{ {
DetailsDialog dialog(tr("Enter Customer Details"), this); DetailsDialog dialog(tr("Enter Customer Details"), this);
if (dialog.exec() == QDialog::Accepted) if (dialog.exec() == QDialog::Accepted) {
createLetter(dialog.senderName(), dialog.senderAddress(), createLetter(dialog.senderName(), dialog.senderAddress(),
dialog.orderItems(), dialog.sendOffers()); dialog.orderItems(), dialog.sendOffers());
}
} }
//! [16] //! [16]

View File

@ -73,4 +73,4 @@ private:
}; };
//! [0] //! [0]
#endif #endif // MAINWINDOW_H

View File

@ -38,8 +38,6 @@
** **
****************************************************************************/ ****************************************************************************/
#include <QtWidgets>
#include "highlighter.h" #include "highlighter.h"
//! [0] //! [0]

View File

@ -42,8 +42,6 @@
#define HIGHLIGHTER_H #define HIGHLIGHTER_H
#include <QSyntaxHighlighter> #include <QSyntaxHighlighter>
#include <QHash>
#include <QTextCharFormat> #include <QTextCharFormat>
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
@ -81,4 +79,4 @@ private:
}; };
//! [0] //! [0]
#endif #endif // HIGHLIGHTER_H

View File

@ -38,10 +38,10 @@
** **
****************************************************************************/ ****************************************************************************/
#include <QApplication>
#include "mainwindow.h" #include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
QApplication app(argc, argv); QApplication app(argc, argv);

View File

@ -74,8 +74,7 @@ void MainWindow::openFile(const QString &path)
QString fileName = path; QString fileName = path;
if (fileName.isNull()) if (fileName.isNull())
fileName = QFileDialog::getOpenFileName(this, fileName = QFileDialog::getOpenFileName(this, tr("Open File"), "", "C++ Files (*.cpp *.h)");
tr("Open File"), "", "C++ Files (*.cpp *.h)");
if (!fileName.isEmpty()) { if (!fileName.isEmpty()) {
QFile file(fileName); QFile file(fileName);
@ -108,14 +107,9 @@ void MainWindow::setupFileMenu()
QMenu *fileMenu = new QMenu(tr("&File"), this); QMenu *fileMenu = new QMenu(tr("&File"), this);
menuBar()->addMenu(fileMenu); menuBar()->addMenu(fileMenu);
fileMenu->addAction(tr("&New"), this, SLOT(newFile()), fileMenu->addAction(tr("&New"), this, SLOT(newFile()), QKeySequence::New);
QKeySequence::New); fileMenu->addAction(tr("&Open..."), this, SLOT(openFile()), QKeySequence::Open);
fileMenu->addAction(tr("E&xit"), qApp, SLOT(quit()), QKeySequence::Quit);
fileMenu->addAction(tr("&Open..."), this, SLOT(openFile()),
QKeySequence::Open);
fileMenu->addAction(tr("E&xit"), qApp, SLOT(quit()),
QKeySequence::Quit);
} }
void MainWindow::setupHelpMenu() void MainWindow::setupHelpMenu()

View File

@ -41,10 +41,10 @@
#ifndef MAINWINDOW_H #ifndef MAINWINDOW_H
#define MAINWINDOW_H #define MAINWINDOW_H
#include <QMainWindow>
#include "highlighter.h" #include "highlighter.h"
#include <QMainWindow>
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
class QTextEdit; class QTextEdit;
QT_END_NAMESPACE QT_END_NAMESPACE
@ -72,4 +72,4 @@ private:
}; };
//! [0] //! [0]
#endif #endif // MAINWINDOW_H

View File

@ -42,13 +42,13 @@
#include "textedit.h" #include "textedit.h"
#include <QApplication> #include <QApplication>
int main( int argc, char ** argv ) int main(int argc, char *argv[])
{ {
Q_INIT_RESOURCE(textedit); Q_INIT_RESOURCE(textedit);
QApplication a( argc, argv ); QApplication a(argc, argv);
TextEdit mw; TextEdit mw;
mw.resize( 700, 800 ); mw.resize(700, 800);
mw.show(); mw.show();
return a.exec(); return a.exec();
} }

View File

@ -39,8 +39,6 @@
** **
****************************************************************************/ ****************************************************************************/
#include "textedit.h"
#include <QAction> #include <QAction>
#include <QApplication> #include <QApplication>
#include <QClipboard> #include <QClipboard>
@ -69,6 +67,8 @@
#include <QPrintPreviewDialog> #include <QPrintPreviewDialog>
#endif #endif
#include "textedit.h"
#ifdef Q_OS_MAC #ifdef Q_OS_MAC
const QString rsrcPath = ":/images/mac"; const QString rsrcPath = ":/images/mac";
#else #else
@ -209,7 +209,7 @@ void TextEdit::setupFileActions()
menu->addAction(a); menu->addAction(a);
a = new QAction(QIcon::fromTheme("exportpdf", QIcon(rsrcPath + "/exportpdf.png")), a = new QAction(QIcon::fromTheme("exportpdf", QIcon(rsrcPath + "/exportpdf.png")),
tr("&Export PDF..."), this); tr("&Export PDF..."), this);
a->setPriority(QAction::LowPriority); a->setPriority(QAction::LowPriority);
a->setShortcut(Qt::CTRL + Qt::Key_D); a->setShortcut(Qt::CTRL + Qt::Key_D);
connect(a, SIGNAL(triggered()), this, SLOT(filePrintPdf())); connect(a, SIGNAL(triggered()), this, SLOT(filePrintPdf()));
@ -291,7 +291,8 @@ void TextEdit::setupTextActions()
menu->addAction(actionTextBold); menu->addAction(actionTextBold);
actionTextBold->setCheckable(true); actionTextBold->setCheckable(true);
actionTextItalic = new QAction(QIcon::fromTheme("format-text-italic", QIcon(rsrcPath + "/textitalic.png")), actionTextItalic = new QAction(QIcon::fromTheme("format-text-italic",
QIcon(rsrcPath + "/textitalic.png")),
tr("&Italic"), this); tr("&Italic"), this);
actionTextItalic->setPriority(QAction::LowPriority); actionTextItalic->setPriority(QAction::LowPriority);
actionTextItalic->setShortcut(Qt::CTRL + Qt::Key_I); actionTextItalic->setShortcut(Qt::CTRL + Qt::Key_I);
@ -303,7 +304,8 @@ void TextEdit::setupTextActions()
menu->addAction(actionTextItalic); menu->addAction(actionTextItalic);
actionTextItalic->setCheckable(true); actionTextItalic->setCheckable(true);
actionTextUnderline = new QAction(QIcon::fromTheme("format-text-underline", QIcon(rsrcPath + "/textunder.png")), actionTextUnderline = new QAction(QIcon::fromTheme("format-text-underline",
QIcon(rsrcPath + "/textunder.png")),
tr("&Underline"), this); tr("&Underline"), this);
actionTextUnderline->setShortcut(Qt::CTRL + Qt::Key_U); actionTextUnderline->setShortcut(Qt::CTRL + Qt::Key_U);
actionTextUnderline->setPriority(QAction::LowPriority); actionTextUnderline->setPriority(QAction::LowPriority);
@ -322,16 +324,29 @@ void TextEdit::setupTextActions()
// Make sure the alignLeft is always left of the alignRight // Make sure the alignLeft is always left of the alignRight
if (QApplication::isLeftToRight()) { if (QApplication::isLeftToRight()) {
actionAlignLeft = new QAction(QIcon::fromTheme("format-justify-left", QIcon(rsrcPath + "/textleft.png")), actionAlignLeft = new QAction(QIcon::fromTheme("format-justify-left",
QIcon(rsrcPath + "/textleft.png")),
tr("&Left"), grp); tr("&Left"), grp);
actionAlignCenter = new QAction(QIcon::fromTheme("format-justify-center", QIcon(rsrcPath + "/textcenter.png")), tr("C&enter"), grp); actionAlignCenter = new QAction(QIcon::fromTheme("format-justify-center",
actionAlignRight = new QAction(QIcon::fromTheme("format-justify-right", QIcon(rsrcPath + "/textright.png")), tr("&Right"), grp); QIcon(rsrcPath + "/textcenter.png")),
tr("C&enter"), grp);
actionAlignRight = new QAction(QIcon::fromTheme("format-justify-right",
QIcon(rsrcPath + "/textright.png")),
tr("&Right"), grp);
} else { } else {
actionAlignRight = new QAction(QIcon::fromTheme("format-justify-right", QIcon(rsrcPath + "/textright.png")), tr("&Right"), grp); actionAlignRight = new QAction(QIcon::fromTheme("format-justify-right",
actionAlignCenter = new QAction(QIcon::fromTheme("format-justify-center", QIcon(rsrcPath + "/textcenter.png")), tr("C&enter"), grp); QIcon(rsrcPath + "/textright.png")),
actionAlignLeft = new QAction(QIcon::fromTheme("format-justify-left", QIcon(rsrcPath + "/textleft.png")), tr("&Left"), grp); tr("&Right"), grp);
actionAlignCenter = new QAction(QIcon::fromTheme("format-justify-center",
QIcon(rsrcPath + "/textcenter.png")),
tr("C&enter"), grp);
actionAlignLeft = new QAction(QIcon::fromTheme("format-justify-left",
QIcon(rsrcPath + "/textleft.png")),
tr("&Left"), grp);
} }
actionAlignJustify = new QAction(QIcon::fromTheme("format-justify-fill", QIcon(rsrcPath + "/textjustify.png")), tr("&Justify"), grp); actionAlignJustify = new QAction(QIcon::fromTheme("format-justify-fill",
QIcon(rsrcPath + "/textjustify.png")),
tr("&Justify"), grp);
actionAlignLeft->setShortcut(Qt::CTRL + Qt::Key_L); actionAlignLeft->setShortcut(Qt::CTRL + Qt::Key_L);
actionAlignLeft->setCheckable(true); actionAlignLeft->setCheckable(true);
@ -358,7 +373,6 @@ void TextEdit::setupTextActions()
tb->addAction(actionTextColor); tb->addAction(actionTextColor);
menu->addAction(actionTextColor); menu->addAction(actionTextColor);
tb = new QToolBar(this); tb = new QToolBar(this);
tb->setAllowedAreas(Qt::TopToolBarArea | Qt::BottomToolBarArea); tb->setAllowedAreas(Qt::TopToolBarArea | Qt::BottomToolBarArea);
tb->setWindowTitle(tr("Format Actions")); tb->setWindowTitle(tr("Format Actions"));
@ -376,13 +390,11 @@ void TextEdit::setupTextActions()
comboStyle->addItem("Ordered List (Alpha upper)"); comboStyle->addItem("Ordered List (Alpha upper)");
comboStyle->addItem("Ordered List (Roman lower)"); comboStyle->addItem("Ordered List (Roman lower)");
comboStyle->addItem("Ordered List (Roman upper)"); comboStyle->addItem("Ordered List (Roman upper)");
connect(comboStyle, SIGNAL(activated(int)), connect(comboStyle, SIGNAL(activated(int)), this, SLOT(textStyle(int)));
this, SLOT(textStyle(int)));
comboFont = new QFontComboBox(tb); comboFont = new QFontComboBox(tb);
tb->addWidget(comboFont); tb->addWidget(comboFont);
connect(comboFont, SIGNAL(activated(QString)), connect(comboFont, SIGNAL(activated(QString)), this, SLOT(textFamily(QString)));
this, SLOT(textFamily(QString)));
comboSize = new QComboBox(tb); comboSize = new QComboBox(tb);
comboSize->setObjectName("comboSize"); comboSize->setObjectName("comboSize");
@ -393,8 +405,7 @@ void TextEdit::setupTextActions()
foreach(int size, db.standardSizes()) foreach(int size, db.standardSizes())
comboSize->addItem(QString::number(size)); comboSize->addItem(QString::number(size));
connect(comboSize, SIGNAL(activated(QString)), connect(comboSize, SIGNAL(activated(QString)), this, SLOT(textSize(QString)));
this, SLOT(textSize(QString)));
comboSize->setCurrentIndex(comboSize->findText(QString::number(QApplication::font() comboSize->setCurrentIndex(comboSize->findText(QString::number(QApplication::font()
.pointSize()))); .pointSize())));
} }
@ -431,8 +442,7 @@ bool TextEdit::maybeSave()
ret = QMessageBox::warning(this, tr("Application"), ret = QMessageBox::warning(this, tr("Application"),
tr("The document has been modified.\n" tr("The document has been modified.\n"
"Do you want to save your changes?"), "Do you want to save your changes?"),
QMessageBox::Save | QMessageBox::Discard QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
| QMessageBox::Cancel);
if (ret == QMessageBox::Save) if (ret == QMessageBox::Save)
return fileSave(); return fileSave();
else if (ret == QMessageBox::Cancel) else if (ret == QMessageBox::Cancel)
@ -485,12 +495,16 @@ bool TextEdit::fileSave()
bool TextEdit::fileSaveAs() bool TextEdit::fileSaveAs()
{ {
QString fn = QFileDialog::getSaveFileName(this, tr("Save as..."), QString fn = QFileDialog::getSaveFileName(this, tr("Save as..."), QString(),
QString(), tr("ODF files (*.odt);;HTML-Files (*.htm *.html);;All Files (*)")); tr("ODF files (*.odt);;HTML-Files "
"(*.htm *.html);;All Files (*)"));
if (fn.isEmpty()) if (fn.isEmpty())
return false; return false;
if (! (fn.endsWith(".odt", Qt::CaseInsensitive) || fn.endsWith(".htm", Qt::CaseInsensitive) || fn.endsWith(".html", Qt::CaseInsensitive)) ) if (!(fn.endsWith(".odt", Qt::CaseInsensitive)
|| fn.endsWith(".htm", Qt::CaseInsensitive)
|| fn.endsWith(".html", Qt::CaseInsensitive))) {
fn += ".odt"; // default fn += ".odt"; // default
}
setCurrentFileName(fn); setCurrentFileName(fn);
return fileSave(); return fileSave();
} }
@ -503,9 +517,8 @@ void TextEdit::filePrint()
if (textEdit->textCursor().hasSelection()) if (textEdit->textCursor().hasSelection())
dlg->addEnabledOption(QAbstractPrintDialog::PrintSelection); dlg->addEnabledOption(QAbstractPrintDialog::PrintSelection);
dlg->setWindowTitle(tr("Print Document")); dlg->setWindowTitle(tr("Print Document"));
if (dlg->exec() == QDialog::Accepted) { if (dlg->exec() == QDialog::Accepted)
textEdit->print(&printer); textEdit->print(&printer);
}
delete dlg; delete dlg;
#endif #endif
} }
@ -724,14 +737,13 @@ void TextEdit::colorChanged(const QColor &c)
void TextEdit::alignmentChanged(Qt::Alignment a) void TextEdit::alignmentChanged(Qt::Alignment a)
{ {
if (a & Qt::AlignLeft) { if (a & Qt::AlignLeft)
actionAlignLeft->setChecked(true); actionAlignLeft->setChecked(true);
} else if (a & Qt::AlignHCenter) { else if (a & Qt::AlignHCenter)
actionAlignCenter->setChecked(true); actionAlignCenter->setChecked(true);
} else if (a & Qt::AlignRight) { else if (a & Qt::AlignRight)
actionAlignRight->setChecked(true); actionAlignRight->setChecked(true);
} else if (a & Qt::AlignJustify) { else if (a & Qt::AlignJustify)
actionAlignJustify->setChecked(true); actionAlignJustify->setChecked(true);
}
} }

View File

@ -46,13 +46,15 @@
#include <QMap> #include <QMap>
#include <QPointer> #include <QPointer>
QT_FORWARD_DECLARE_CLASS(QAction) QT_BEGIN_NAMESPACE
QT_FORWARD_DECLARE_CLASS(QComboBox) class QAction;
QT_FORWARD_DECLARE_CLASS(QFontComboBox) class QComboBox;
QT_FORWARD_DECLARE_CLASS(QTextEdit) class QFontComboBox;
QT_FORWARD_DECLARE_CLASS(QTextCharFormat) class QTextEdit;
QT_FORWARD_DECLARE_CLASS(QMenu) class QTextCharFormat;
QT_FORWARD_DECLARE_CLASS(QPrinter) class QMenu;
class QPrinter;
QT_END_NAMESPACE
class TextEdit : public QMainWindow class TextEdit : public QMainWindow
{ {
@ -103,20 +105,20 @@ private:
void colorChanged(const QColor &c); void colorChanged(const QColor &c);
void alignmentChanged(Qt::Alignment a); void alignmentChanged(Qt::Alignment a);
QAction *actionSave, QAction *actionSave;
*actionTextBold, QAction *actionTextBold;
*actionTextUnderline, QAction *actionTextUnderline;
*actionTextItalic, QAction *actionTextItalic;
*actionTextColor, QAction *actionTextColor;
*actionAlignLeft, QAction *actionAlignLeft;
*actionAlignCenter, QAction *actionAlignCenter;
*actionAlignRight, QAction *actionAlignRight;
*actionAlignJustify, QAction *actionAlignJustify;
*actionUndo, QAction *actionUndo;
*actionRedo, QAction *actionRedo;
*actionCut, QAction *actionCut;
*actionCopy, QAction *actionCopy;
*actionPaste; QAction *actionPaste;
QComboBox *comboStyle; QComboBox *comboStyle;
QFontComboBox *comboFont; QFontComboBox *comboFont;
@ -127,4 +129,4 @@ private:
QTextEdit *textEdit; QTextEdit *textEdit;
}; };
#endif #endif // TEXTEDIT_H