mainwindow.cpp
来自「一款基于QT开发的流程图绘制程序」· C++ 代码 · 共 653 行 · 第 1/2 页
CPP
653 行
//! [21]
QToolButton *textButton = new QToolButton;
textButton->setCheckable(true);
buttonGroup->addButton(textButton, InsertTextButton);
textButton->setIcon(QIcon(QPixmap(":/images/textpointer.png")
.scaled(30, 30)));
textButton->setIconSize(QSize(50, 50));
QGridLayout *textLayout = new QGridLayout;
textLayout->addWidget(textButton, 0, 0, Qt::AlignHCenter);
textLayout->addWidget(new QLabel(tr("Text")), 1, 0, Qt::AlignCenter);
QWidget *textWidget = new QWidget;
textWidget->setLayout(textLayout);
layout->addWidget(textWidget, 1, 1);
layout->setRowStretch(3, 10);
layout->setColumnStretch(2, 10);
QWidget *itemWidget = new QWidget;
itemWidget->setLayout(layout);
backgroundButtonGroup = new QButtonGroup;
connect(backgroundButtonGroup, SIGNAL(buttonClicked(QAbstractButton *)),
this, SLOT(backgroundButtonGroupClicked(QAbstractButton *)));
QGridLayout *backgroundLayout = new QGridLayout;
backgroundLayout->addWidget(createBackgroundCellWidget(tr("Blue Grid"),
":/images/background1.png"), 0, 0);
backgroundLayout->addWidget(createBackgroundCellWidget(tr("White Grid"),
":/images/background2.png"), 0, 1);
backgroundLayout->addWidget(createBackgroundCellWidget(tr("Gray Grid"),
":/images/background3.png"), 1, 0);
backgroundLayout->addWidget(createBackgroundCellWidget(tr("No Grid"),
":/images/background4.png"), 1, 1);
backgroundLayout->setRowStretch(2, 10);
backgroundLayout->setColumnStretch(2, 10);
QWidget *backgroundWidget = new QWidget;
backgroundWidget->setLayout(backgroundLayout);
//! [22]
toolBox = new QToolBox;
toolBox->setSizePolicy(QSizePolicy(QSizePolicy::Maximum, QSizePolicy::Ignored));
toolBox->setMinimumWidth(itemWidget->sizeHint().width());
toolBox->addItem(itemWidget, tr("Basic Flowchart Shapes"));
toolBox->addItem(backgroundWidget, tr("Backgrounds"));
}
//! [22]
//! [23]
void MainWindow::createActions()
{
toFrontAction = new QAction(QIcon(":/images/bringtofront.png"),
tr("Bring to &Front"), this);
toFrontAction->setShortcut(tr("Ctrl+F"));
toFrontAction->setStatusTip(tr("Bring item to front"));
connect(toFrontAction, SIGNAL(triggered()),
this, SLOT(bringToFront()));
//! [23]
sendBackAction = new QAction(QIcon(":/images/sendtoback.png"),
tr("Send to &Back"), this);
sendBackAction->setShortcut(tr("Ctrl+B"));
sendBackAction->setStatusTip(tr("Send item to back"));
connect(sendBackAction, SIGNAL(triggered()),
this, SLOT(sendToBack()));
deleteAction = new QAction(QIcon(":/images/delete.png"),
tr("&Delete"), this);
deleteAction->setShortcut(tr("Delete"));
deleteAction->setStatusTip(tr("Delete item from diagram"));
connect(deleteAction, SIGNAL(triggered()),
this, SLOT(deleteItem()));
exitAction = new QAction(tr("E&xit"), this);
exitAction->setShortcut(tr("Ctrl+X"));
exitAction->setStatusTip(tr("Quit Scenediagram example"));
connect(exitAction, SIGNAL(triggered()), this, SLOT(close()));
boldAction = new QAction(tr("Bold"), this);
boldAction->setCheckable(true);
QPixmap pixmap(":/images/bold.png");
boldAction->setIcon(QIcon(pixmap));
boldAction->setShortcut(tr("Ctrl+B"));
connect(boldAction, SIGNAL(triggered()),
this, SLOT(handleFontChange()));
italicAction = new QAction(QIcon(":/images/italic.png"),
tr("Italic"), this);
italicAction->setCheckable(true);
italicAction->setShortcut(tr("Ctrl+I"));
connect(italicAction, SIGNAL(triggered()),
this, SLOT(handleFontChange()));
underlineAction = new QAction(QIcon(":/images/underline.png"),
tr("Underline"), this);
underlineAction->setCheckable(true);
underlineAction->setShortcut(tr("Ctrl+U"));
connect(underlineAction, SIGNAL(triggered()),
this, SLOT(handleFontChange()));
aboutAction = new QAction(tr("A&bout"), this);
aboutAction->setShortcut(tr("Ctrl+B"));
connect(aboutAction, SIGNAL(triggered()),
this, SLOT(about()));
}
//! [24]
void MainWindow::createMenus()
{
fileMenu = menuBar()->addMenu(tr("&File"));
fileMenu->addAction(exitAction);
itemMenu = menuBar()->addMenu(tr("&Item"));
itemMenu->addAction(deleteAction);
itemMenu->addSeparator();
itemMenu->addAction(toFrontAction);
itemMenu->addAction(sendBackAction);
aboutMenu = menuBar()->addMenu(tr("&Help"));
aboutMenu->addAction(aboutAction);
}
//! [24]
//! [25]
void MainWindow::createToolbars()
{
//! [25]
editToolBar = addToolBar(tr("Edit"));
editToolBar->addAction(deleteAction);
editToolBar->addAction(toFrontAction);
editToolBar->addAction(sendBackAction);
fontCombo = new QFontComboBox();
fontSizeCombo = new QComboBox();
connect(fontCombo, SIGNAL(currentFontChanged(const QFont &)),
this, SLOT(currentFontChanged(const QFont &)));
fontSizeCombo = new QComboBox;
fontSizeCombo->setEditable(true);
for (int i = 8; i < 30; i = i + 2)
fontSizeCombo->addItem(QString().setNum(i));
QIntValidator *validator = new QIntValidator(2, 64, this);
fontSizeCombo->setValidator(validator);
connect(fontSizeCombo, SIGNAL(currentIndexChanged(const QString &)),
this, SLOT(fontSizeChanged(const QString &)));
fontColorToolButton = new QToolButton;
fontColorToolButton->setPopupMode(QToolButton::MenuButtonPopup);
fontColorToolButton->setMenu(createColorMenu(SLOT(textColorChanged()),
Qt::black));
textAction = fontColorToolButton->menu()->defaultAction();
fontColorToolButton->setIcon(createColorToolButtonIcon(
":/images/textpointer.png", Qt::black));
fontColorToolButton->setAutoFillBackground(true);
connect(fontColorToolButton, SIGNAL(clicked()),
this, SLOT(textButtonTriggered()));
//! [26]
fillColorToolButton = new QToolButton;
fillColorToolButton->setPopupMode(QToolButton::MenuButtonPopup);
fillColorToolButton->setMenu(createColorMenu(SLOT(itemColorChanged()),
Qt::white));
fillAction = fillColorToolButton->menu()->defaultAction();
fillColorToolButton->setIcon(createColorToolButtonIcon(
":/images/floodfill.png", Qt::white));
connect(fillColorToolButton, SIGNAL(clicked()),
this, SLOT(fillButtonTriggered()));
//! [26]
lineColorToolButton = new QToolButton;
lineColorToolButton->setPopupMode(QToolButton::MenuButtonPopup);
lineColorToolButton->setMenu(createColorMenu(SLOT(lineColorChanged()),
Qt::black));
lineAction = lineColorToolButton->menu()->defaultAction();
lineColorToolButton->setIcon(createColorToolButtonIcon(
":/images/linecolor.png", Qt::black));
connect(lineColorToolButton, SIGNAL(clicked()),
this, SLOT(lineButtonTriggered()));
textToolBar = addToolBar(tr("Font"));
textToolBar->addWidget(fontCombo);
textToolBar->addWidget(fontSizeCombo);
textToolBar->addAction(boldAction);
textToolBar->addAction(italicAction);
textToolBar->addAction(underlineAction);
colorToolBar = addToolBar(tr("Color"));
colorToolBar->addWidget(fontColorToolButton);
colorToolBar->addWidget(fillColorToolButton);
colorToolBar->addWidget(lineColorToolButton);
QToolButton *pointerButton = new QToolButton;
pointerButton->setCheckable(true);
pointerButton->setChecked(true);
pointerButton->setIcon(QIcon(":/images/pointer.png"));
QToolButton *linePointerButton = new QToolButton;
linePointerButton->setCheckable(true);
linePointerButton->setIcon(QIcon(":/images/linepointer.png"));
pointerTypeGroup = new QButtonGroup;
pointerTypeGroup->addButton(pointerButton, int(DiagramScene::MoveItem));
pointerTypeGroup->addButton(linePointerButton,
int(DiagramScene::InsertLine));
connect(pointerTypeGroup, SIGNAL(buttonClicked(int)),
this, SLOT(pointerGroupClicked(int)));
sceneScaleCombo = new QComboBox;
QStringList scales;
scales << tr("50%") << tr("75%") << tr("100%") << tr("125%") << tr("150%");
sceneScaleCombo->addItems(scales);
sceneScaleCombo->setCurrentIndex(2);
connect(sceneScaleCombo, SIGNAL(currentIndexChanged(const QString &)),
this, SLOT(sceneScaleChanged(const QString &)));
pointerToolbar = addToolBar(tr("Pointer type"));
pointerToolbar->addWidget(pointerButton);
pointerToolbar->addWidget(linePointerButton);
pointerToolbar->addWidget(sceneScaleCombo);
//! [27]
}
//! [27]
//! [28]
QWidget *MainWindow::createBackgroundCellWidget(const QString &text,
const QString &image)
{
QToolButton *button = new QToolButton;
button->setText(text);
button->setIcon(QIcon(image));
button->setIconSize(QSize(50, 50));
button->setCheckable(true);
backgroundButtonGroup->addButton(button);
QGridLayout *layout = new QGridLayout;
layout->addWidget(button, 0, 0, Qt::AlignHCenter);
layout->addWidget(new QLabel(text), 1, 0, Qt::AlignCenter);
QWidget *widget = new QWidget;
widget->setLayout(layout);
return widget;
}
//! [28]
//! [29]
QWidget *MainWindow::createCellWidget(const QString &text,
DiagramItem::DiagramType type)
{
DiagramItem item(type, itemMenu);
QIcon icon(item.image());
QToolButton *button = new QToolButton;
button->setIcon(icon);
button->setIconSize(QSize(50, 50));
button->setCheckable(true);
buttonGroup->addButton(button, int(type));
QGridLayout *layout = new QGridLayout;
layout->addWidget(button, 0, 0, Qt::AlignHCenter);
layout->addWidget(new QLabel(text), 1, 0, Qt::AlignCenter);
QWidget *widget = new QWidget;
widget->setLayout(layout);
return widget;
}
//! [29]
//! [30]
QMenu *MainWindow::createColorMenu(const char *slot, QColor defaultColor)
{
QList<QColor> colors;
colors << Qt::black << Qt::white << Qt::red << Qt::blue << Qt::yellow;
QStringList names;
names << tr("black") << tr("white") << tr("red") << tr("blue")
<< tr("yellow");
QMenu *colorMenu = new QMenu;
for (int i = 0; i < colors.count(); ++i) {
QAction *action = new QAction(names.at(i), this);
action->setData(colors.at(i));
action->setIcon(createColorIcon(colors.at(i)));
connect(action, SIGNAL(triggered()),
this, slot);
colorMenu->addAction(action);
if (colors.at(i) == defaultColor) {
colorMenu->setDefaultAction(action);
}
}
return colorMenu;
}
//! [30]
//! [31]
QIcon MainWindow::createColorToolButtonIcon(const QString &imageFile,
QColor color)
{
QPixmap pixmap(50, 80);
pixmap.fill(Qt::transparent);
QPainter painter(&pixmap);
QPixmap image(imageFile);
QRect target(0, 0, 50, 60);
QRect source(0, 0, 42, 42);
painter.fillRect(QRect(0, 60, 50, 80), color);
painter.drawPixmap(target, image, source);
return QIcon(pixmap);
}
//! [31]
//! [32]
QIcon MainWindow::createColorIcon(QColor color)
{
QPixmap pixmap(20, 20);
QPainter painter(&pixmap);
painter.setPen(Qt::NoPen);
painter.fillRect(QRect(0, 0, 20, 20), color);
return QIcon(pixmap);
}
//! [32]
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?