From 784f6fef7d66929dd55bd40e8019637622ddc17f Mon Sep 17 00:00:00 2001 From: Thomas Leitz Date: Sat, 2 Feb 2019 10:28:09 +0100 Subject: [PATCH] change font of selection --- src/commands.cpp | 26 ++++++++++++++++++++++++++ src/commands.h | 13 +++++++++++++ src/mainwindow.cpp | 4 +--- src/page.cpp | 19 +++++++++++++++++++ src/page.h | 2 ++ src/widget.cpp | 15 ++++++++++++++- src/widget.h | 1 + 7 files changed, 76 insertions(+), 4 deletions(-) diff --git a/src/commands.cpp b/src/commands.cpp index 4dec7b8..4e44606 100644 --- a/src/commands.cpp +++ b/src/commands.cpp @@ -217,6 +217,32 @@ bool TransformSelectionCommand::mergeWith(const QUndoCommand *other) return true; } +/****************************************************************************** +** ChangeFontOfSelectionCommand +*/ + +ChangeFontOfSelectionCommand::ChangeFontOfSelectionCommand(Widget *widget, QFont font, QUndoCommand *parent) : QUndoCommand(parent) +{ + setText(MainWindow::tr("Change Font")); + + m_widget = widget; + m_selection = m_widget->currentSelection; + m_font = font; +} + +void ChangeFontOfSelectionCommand::undo() +{ + m_widget->currentSelection = m_selection; +} + +void ChangeFontOfSelectionCommand::redo() +{ + for (size_t i = 0; i < m_widget->currentSelection.elements().size(); ++i) + { + m_widget->currentSelection.changeFont(i, m_font); + } +} + /****************************************************************************** ** ChangeColorOfSelectionCommand */ diff --git a/src/commands.h b/src/commands.h index be36ed7..bcd467a 100644 --- a/src/commands.h +++ b/src/commands.h @@ -86,6 +86,19 @@ class TransformSelectionCommand : public QUndoCommand size_t pageNum; }; +class ChangeFontOfSelectionCommand : public QUndoCommand +{ +public: + ChangeFontOfSelectionCommand(Widget *widget, QFont font, QUndoCommand *parent = nullptr); + void undo() Q_DECL_OVERRIDE; + void redo() Q_DECL_OVERRIDE; + +private: + Widget *m_widget; + MrDoc::Selection m_selection; + QFont m_font; +}; + class ChangeColorOfSelectionCommand : public QUndoCommand { public: diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 4ccd0cf..df186fe 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -351,8 +351,6 @@ void MainWindow::createActions() fontAct = new QAction(tr("Select Font"), this); fontAct->setStatusTip(tr("Select Font")); - fontAct->setCheckable(true); - fontAct->setChecked(false); connect(fontAct, SIGNAL(triggered()), this, SLOT(font())); eraserAct = new QAction(QIcon(":/images/eraserIcon.png"), tr("Eraser"), this); @@ -1115,7 +1113,7 @@ void MainWindow::font() bool ok; QFont font = QFontDialog::getFont(&ok, mainWidget->m_currentFont, this); if (ok) { - mainWidget->m_currentFont = font; + mainWidget->setCurrentFont(font); } updateGUI(); } diff --git a/src/page.cpp b/src/page.cpp index 18f7781..ff4b7a5 100644 --- a/src/page.cpp +++ b/src/page.cpp @@ -132,6 +132,25 @@ bool Page::changePenWidth(size_t elementNum, qreal penWidth) return false; } +bool Page::changeFont(size_t elementNum, QFont font) +{ + if (elementNum >= m_elements.size() || m_elements.empty()) + { + return false; + } + else + { + auto text = dynamic_cast(m_elements[elementNum].get()); + if (nullptr != text) + { + text->m_font = font; + m_dirtyRect = m_dirtyRect.united(text->boundingRect()); + return true; + } + } + return false; +} + bool Page::changeStrokeColor(size_t elementNum, QColor color) { if (elementNum >= m_elements.size() || m_elements.empty()) diff --git a/src/page.h b/src/page.h index 9bacea2..44f8501 100644 --- a/src/page.h +++ b/src/page.h @@ -31,6 +31,8 @@ class Page const QRectF &dirtyRect() const; void clearDirtyRect(); + bool changeFont(size_t elementNum, QFont font); + bool changePenWidth(size_t strokeNum, qreal penWidth); bool changeStrokeColor(size_t strokeNum, QColor color); bool changeStrokePattern(size_t strokeNum, QVector pattern); diff --git a/src/widget.cpp b/src/widget.cpp index 3c69d52..2dfd70e 100644 --- a/src/widget.cpp +++ b/src/widget.cpp @@ -2354,12 +2354,25 @@ Widget::state Widget::getCurrentState() return currentState; } +void Widget::setCurrentFont(QFont font) +{ + m_currentFont = font; + if (currentState == state::SELECTED) + { + auto changeFontCommand = new ChangeFontOfSelectionCommand(this, font); + undoStack.push(changeFontCommand); + currentSelection.finalize(); + currentSelection.updateBuffer(m_zoom); + update(); + } +} + void Widget::setCurrentColor(QColor newColor) { currentColor = newColor; if (currentState == state::SELECTED) { - ChangeColorOfSelectionCommand *changeColorCommand = new ChangeColorOfSelectionCommand(this, newColor); + auto changeColorCommand = new ChangeColorOfSelectionCommand(this, newColor); undoStack.push(changeColorCommand); currentSelection.updateBuffer(m_zoom); update(); diff --git a/src/widget.h b/src/widget.h index 124e82e..6095f16 100644 --- a/src/widget.h +++ b/src/widget.h @@ -119,6 +119,7 @@ class Widget : public QWidget void setCurrentState(state newState); state getCurrentState(); + void setCurrentFont(QFont font); void setCurrentColor(QColor newColor); QColor getCurrentColor();