Skip to content

Commit

Permalink
change font of selection
Browse files Browse the repository at this point in the history
  • Loading branch information
unruhschuh committed Feb 2, 2019
1 parent 2c06ec4 commit 784f6fe
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 4 deletions.
26 changes: 26 additions & 0 deletions src/commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
13 changes: 13 additions & 0 deletions src/commands.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 1 addition & 3 deletions src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();
}
Expand Down
19 changes: 19 additions & 0 deletions src/page.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Text*>(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())
Expand Down
2 changes: 2 additions & 0 deletions src/page.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<qreal> pattern);
Expand Down
15 changes: 14 additions & 1 deletion src/widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions src/widget.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ class Widget : public QWidget
void setCurrentState(state newState);
state getCurrentState();

void setCurrentFont(QFont font);
void setCurrentColor(QColor newColor);
QColor getCurrentColor();

Expand Down

0 comments on commit 784f6fe

Please sign in to comment.