📄 mainwindow.cpp
字号:
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); 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"));}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())); 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()));}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);}void MainWindow::createToolbars(){ 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())); 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())); 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);}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;}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;}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;}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);}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);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -