Skip to content

Commit 5ccaf03

Browse files
committed
Fix GitHub Issue #138: Make it possible to change the font
1 parent 54dd42d commit 5ccaf03

22 files changed

+372
-57
lines changed

CHANGELOG

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ x.y.z
44
New features:
55

66
* Fix GitHub Issue #36: Double-click to zoom into a node
7+
* Fix GitHub Issue #138: Make it possible to change the font
78
* Fix GitHub Issue #180: Make edge text disappear if it doesn't fit between nodes
89

910
Bug fixes:

src/alz_serializer.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,20 @@ const auto EDGE_COLOR = "edge-color";
4646

4747
const auto EDGE_THICKNESS = "edge-width";
4848

49+
const auto FONT_FAMILY = "font-family";
50+
51+
const auto FONT_BOLD = "bold";
52+
53+
const auto FONT_OVERLINE = "overline";
54+
55+
const auto FONT_STRIKE_OUT = "strike-out";
56+
57+
const auto FONT_UNDERLINE = "underline";
58+
59+
const auto FONT_WEIGHT = "weight";
60+
61+
const auto FONT_ITALIC = "italic";
62+
4963
const auto GRAPH = "graph";
5064

5165
const auto GRID_COLOR = "grid-color";
@@ -430,6 +444,17 @@ std::unique_ptr<MindMapData> fromXml(QDomDocument document)
430444
{ QString(DataKeywords::MindMap::EDGE_COLOR), [&data](const QDomElement & e) {
431445
data->setEdgeColor(readColorElement(e));
432446
} },
447+
{ QString(DataKeywords::MindMap::FONT_FAMILY), [&data](const QDomElement & e) {
448+
QFont font;
449+
font.setFamily(readFirstTextNodeContent(e));
450+
font.setBold(e.attribute(DataKeywords::MindMap::FONT_BOLD).toInt());
451+
font.setItalic(e.attribute(DataKeywords::MindMap::FONT_ITALIC).toUInt());
452+
font.setOverline(e.attribute(DataKeywords::MindMap::FONT_OVERLINE).toInt());
453+
font.setUnderline(e.attribute(DataKeywords::MindMap::FONT_UNDERLINE).toInt());
454+
font.setStrikeOut(e.attribute(DataKeywords::MindMap::FONT_STRIKE_OUT).toInt());
455+
font.setWeight(e.attribute(DataKeywords::MindMap::FONT_WEIGHT).toInt());
456+
data->setFont(font);
457+
} },
433458
{ QString(DataKeywords::MindMap::GRID_COLOR), [&data](const QDomElement & e) {
434459
data->setGridColor(readColorElement(e));
435460
} },
@@ -476,6 +501,16 @@ QDomDocument toXml(MindMapData & mindMapData)
476501
edgeWidthElement.appendChild(doc.createTextNode(QString::number(static_cast<int>(mindMapData.edgeWidth() * SCALE))));
477502
root.appendChild(edgeWidthElement);
478503

504+
auto fontFamilyElement = doc.createElement(DataKeywords::MindMap::FONT_FAMILY);
505+
fontFamilyElement.setAttribute(DataKeywords::MindMap::FONT_BOLD, mindMapData.font().bold());
506+
fontFamilyElement.setAttribute(DataKeywords::MindMap::FONT_ITALIC, mindMapData.font().italic());
507+
fontFamilyElement.setAttribute(DataKeywords::MindMap::FONT_OVERLINE, mindMapData.font().overline());
508+
fontFamilyElement.setAttribute(DataKeywords::MindMap::FONT_STRIKE_OUT, mindMapData.font().strikeOut());
509+
fontFamilyElement.setAttribute(DataKeywords::MindMap::FONT_UNDERLINE, mindMapData.font().underline());
510+
fontFamilyElement.setAttribute(DataKeywords::MindMap::FONT_WEIGHT, mindMapData.font().weight());
511+
fontFamilyElement.appendChild(doc.createTextNode(mindMapData.font().family()));
512+
root.appendChild(fontFamilyElement);
513+
479514
auto textSizeElement = doc.createElement(DataKeywords::MindMap::TEXT_SIZE);
480515
textSizeElement.appendChild(doc.createTextNode(QString::number(static_cast<int>(mindMapData.textSize() * SCALE))));
481516
root.appendChild(textSizeElement);

src/application.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ Application::Application(int & argc, char ** argv)
173173

174174
connect(m_mainWindow.get(), &MainWindow::cornerRadiusChanged, m_mediator.get(), &Mediator::setCornerRadius);
175175
connect(m_mainWindow.get(), &MainWindow::edgeWidthChanged, m_mediator.get(), &Mediator::setEdgeWidth);
176+
connect(m_mainWindow.get(), &MainWindow::fontChanged, m_mediator.get(), &Mediator::setFont);
176177
connect(m_mainWindow.get(), &MainWindow::textSizeChanged, m_mediator.get(), &Mediator::setTextSize);
177178
connect(m_mainWindow.get(), &MainWindow::gridSizeChanged, m_editorView, &EditorView::setGridSize);
178179
connect(m_mainWindow.get(), &MainWindow::gridVisibleChanged, [this](int state) {

src/constants.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ namespace Text {
192192

193193
static const int MIN_SIZE = 6;
194194

195-
static const int MAX_SIZE = 24;
195+
static const int MAX_SIZE = 72;
196196

197197
} // namespace Text
198198

src/edge.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "simple_logger.hpp"
2828

2929
#include <QBrush>
30+
#include <QFont>
3031
#include <QGraphicsEllipseItem>
3132
#include <QPen>
3233
#include <QPropertyAnimation>
@@ -192,6 +193,16 @@ void Edge::setText(const QString & text)
192193
}
193194
}
194195

196+
void Edge::setFont(const QFont & font)
197+
{
198+
if (m_label) {
199+
// Handle size and family separately to maintain backwards compatibility
200+
QFont newFont(font);
201+
newFont.setPointSize(m_label->font().pointSize());
202+
m_label->setFont(newFont);
203+
}
204+
}
205+
195206
void Edge::setTextSize(int textSize)
196207
{
197208
if (m_label) {

src/edge.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
class EdgeDot;
2929
class Node;
30+
class QFont;
3031
class QGraphicsEllipseItem;
3132
class QPropertyAnimation;
3233

@@ -73,6 +74,8 @@ public slots:
7374

7475
void setColor(const QColor & color);
7576

77+
void setFont(const QFont & font);
78+
7679
void setWidth(double width);
7780

7881
void setText(const QString & text);

src/main_window.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,14 @@
3131
#include <QApplication>
3232
#include <QCheckBox>
3333
#include <QDoubleSpinBox>
34+
#include <QFontDialog>
3435
#include <QHBoxLayout>
3536
#include <QLabel>
3637
#include <QLineEdit>
3738
#include <QMenu>
3839
#include <QMenuBar>
3940
#include <QMessageBox>
41+
#include <QPushButton>
4042
#include <QScreen>
4143
#include <QSpinBox>
4244
#include <QToolBar>
@@ -63,6 +65,7 @@ MainWindow::MainWindow()
6365
, m_redoAction(new QAction(tr("Redo"), this))
6466
, m_edgeWidthSpinBox(new QDoubleSpinBox(this))
6567
, m_cornerRadiusSpinBox(new QSpinBox(this))
68+
, m_fontButton(new QPushButton(this))
6669
, m_gridSizeSpinBox(new QSpinBox(this))
6770
, m_textSizeSpinBox(new QSpinBox(this))
6871
, m_copyOnDragCheckBox(new QCheckBox(tr("Copy on drag"), this))
@@ -234,6 +237,27 @@ QWidgetAction * MainWindow::createTextSizeAction()
234237
return WidgetFactory::buildToolBarWidgetActionWithLabel(tr("Text size:"), *m_textSizeSpinBox, *this).second;
235238
}
236239

240+
QWidgetAction * MainWindow::createFontAction()
241+
{
242+
m_fontButton->setText(tr("Font") + threeDots);
243+
connect(m_fontButton, &QPushButton::clicked, [=] {
244+
bool ok;
245+
QFont defaultFont = m_fontButton->font();
246+
defaultFont.setPointSize(m_textSizeSpinBox->value());
247+
const auto font = QFontDialog::getFont(&ok, defaultFont, this);
248+
if (ok) {
249+
// Note: Support for multiple families implemented in Qt 5.13 =>
250+
juzzlin::L().debug() << "Font family selected: '" << font.family().toStdString() << "'";
251+
juzzlin::L().debug() << "Font weight selected: " << font.weight();
252+
updateFontButtonFont(font);
253+
m_textSizeSpinBox->setValue(font.pointSize());
254+
emit textSizeChanged(font.pointSize());
255+
emit fontChanged(font);
256+
}
257+
});
258+
return WidgetFactory::buildToolBarWidgetAction(*m_fontButton, *this).second;
259+
}
260+
237261
QWidgetAction * MainWindow::createGridSizeAction()
238262
{
239263
m_gridSizeSpinBox->setMinimum(Constants::Grid::MIN_SIZE);
@@ -412,6 +436,8 @@ void MainWindow::createToolBar()
412436
toolBar->addSeparator();
413437
toolBar->addAction(createTextSizeAction());
414438
toolBar->addSeparator();
439+
toolBar->addAction(createFontAction());
440+
toolBar->addSeparator();
415441
toolBar->addAction(createCornerRadiusAction());
416442
toolBar->addSeparator();
417443
toolBar->addAction(createGridSizeAction());
@@ -552,6 +578,13 @@ void MainWindow::populateMenuBar()
552578
createHelpMenu();
553579
}
554580

581+
void MainWindow::updateFontButtonFont(const QFont & font)
582+
{
583+
QFont newFont(font);
584+
newFont.setPointSize(m_fontButton->font().pointSize());
585+
m_fontButton->setFont(newFont);
586+
}
587+
555588
void MainWindow::appear()
556589
{
557590
if (Settings::loadFullScreen()) {
@@ -604,6 +637,11 @@ void MainWindow::setEdgeWidth(double value)
604637
}
605638
}
606639

640+
void MainWindow::setFont(const QFont & font)
641+
{
642+
updateFontButtonFont(font);
643+
}
644+
607645
void MainWindow::setTextSize(int textSize)
608646
{
609647
if (m_textSizeSpinBox->value() != textSize) {

src/main_window.hpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class QAction;
3535
class QCheckBox;
3636
class QDoubleSpinBox;
3737
class QLineEdit;
38+
class QPushButton;
3839
class QSlider;
3940
class QSpinBox;
4041
class QTextEdit;
@@ -92,6 +93,8 @@ public slots:
9293

9394
void setEdgeWidth(double value);
9495

96+
void setFont(const QFont & font);
97+
9598
void setTextSize(int textSize);
9699

97100
void showErrorDialog(QString message);
@@ -107,6 +110,8 @@ public slots:
107110

108111
void edgeWidthChanged(double width);
109112

113+
void fontChanged(QFont font);
114+
110115
void gridSizeChanged(int size);
111116

112117
void gridVisibleChanged(int state);
@@ -134,6 +139,8 @@ public slots:
134139

135140
QWidgetAction * createEdgeWidthAction();
136141

142+
QWidgetAction * createFontAction();
143+
137144
QWidgetAction * createGridSizeAction();
138145

139146
QWidgetAction * createSearchAction();
@@ -156,6 +163,8 @@ public slots:
156163

157164
void populateMenuBar();
158165

166+
void updateFontButtonFont(const QFont & font);
167+
159168
AboutDlg * m_aboutDlg;
160169

161170
SettingsDialog * m_settingsDlg;
@@ -180,6 +189,8 @@ public slots:
180189

181190
QSpinBox * m_cornerRadiusSpinBox = nullptr;
182191

192+
QPushButton * m_fontButton = nullptr;
193+
183194
QSpinBox * m_gridSizeSpinBox = nullptr;
184195

185196
QSpinBox * m_textSizeSpinBox = nullptr;

src/mediator.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ void Mediator::addExistingGraphToScene()
5757
addItem(*node);
5858
node->setCornerRadius(m_editorData->mindMapData()->cornerRadius());
5959
node->setTextSize(m_editorData->mindMapData()->textSize());
60+
node->setFont(m_editorData->mindMapData()->font());
6061
L().debug() << "Added existing node " << node->index() << " to scene";
6162
}
6263
}
@@ -69,6 +70,7 @@ void Mediator::addExistingGraphToScene()
6970
edge->setColor(m_editorData->mindMapData()->edgeColor());
7071
edge->setWidth(m_editorData->mindMapData()->edgeWidth());
7172
edge->setTextSize(m_editorData->mindMapData()->textSize());
73+
edge->setFont(m_editorData->mindMapData()->font());
7274
node0->addGraphicsEdge(*edge);
7375
node1->addGraphicsEdge(*edge);
7476
edge->updateLine();
@@ -82,6 +84,7 @@ void Mediator::addExistingGraphToScene()
8284
m_mainWindow.setCornerRadius(m_editorData->mindMapData()->cornerRadius());
8385
m_mainWindow.setEdgeWidth(m_editorData->mindMapData()->edgeWidth());
8486
m_mainWindow.setTextSize(m_editorData->mindMapData()->textSize());
87+
m_mainWindow.setFont(m_editorData->mindMapData()->font());
8588

8689
m_editorView->setCornerRadius(m_editorData->mindMapData()->cornerRadius());
8790
m_editorView->setEdgeColor(m_editorData->mindMapData()->edgeColor());
@@ -695,6 +698,12 @@ void Mediator::setSearchText(QString text)
695698
updateNodeConnectionActions();
696699
}
697700

701+
void Mediator::setFont(QFont font)
702+
{
703+
saveUndoPoint();
704+
m_editorData->mindMapData()->setFont(font);
705+
}
706+
698707
void Mediator::setTextSize(int textSize)
699708
{
700709
// Break loop with the spinbox

src/mediator.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#ifndef MEDIATOR_HPP
1717
#define MEDIATOR_HPP
1818

19+
#include <QFont>
1920
#include <QObject>
2021
#include <QPointF>
2122
#include <QString>
@@ -147,6 +148,8 @@ class Mediator : public QObject
147148

148149
void setEditorView(EditorView & editorView);
149150

151+
void setFont(QFont font);
152+
150153
//! \returns number of nodes in the current rectangle.
151154
size_t setRectagleSelection(QRectF rect);
152155

0 commit comments

Comments
 (0)