1 /*
2  * Copyright (C) Pedram Pourang (aka Tsu Jan) 2019 <tsujan2000@gmail.com>
3  *
4  * FeatherPad is free software: you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the
6  * Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * FeatherPad is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12  * See the GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program.  If not, see <http://www.gnu.org/licenses/>.
16  *
17  * @license GPL-3.0+ <https://spdx.org/licenses/GPL-3.0+.html>
18  */
19 
20 #include "fontDialog.h"
21 #include "ui_fontDialog.h"
22 
23 namespace FeatherPad {
24 
FontDialog(const QFont & font,QWidget * parent)25 FontDialog::FontDialog (const QFont &font, QWidget *parent)
26     : QDialog (parent),
27       ui (new Ui::FontDialog),
28       font_ (font)
29 {
30     ui->setupUi (this);
31     int widthHint = sizeHint().width(); // before hiding one of combo boxes
32 
33     /* a coding font should be normal and have a fixed pitch */
34     bool codingFont = !font_.italic() && font_.weight() == QFont::Normal
35                       && QFontInfo (font_).fixedPitch();
36 
37     ui->monoFontComboBox->setCurrentFont (font_); // may select another font
38     ui->fontComboBox->setCurrentFont (font_);
39     if (codingFont)
40     {
41         ui->codingFontBox->setChecked (true);
42         ui->fontComboBox->hide();
43         ui->fontLabel->hide();
44         ui->monoFontComboBox->show();
45         ui->monoFontLabel->show();
46     }
47     else
48     {
49         ui->codingFontBox->setChecked (false);
50         ui->monoFontComboBox->hide();
51         ui->monoFontLabel->hide();
52         ui->fontComboBox->show();
53         ui->fontLabel->show();
54 
55         ui->italicBox->setChecked (font_.italic());
56 
57         int weight = font_.weight();
58         switch (weight) {
59         case QFont::Normal:
60             ui->weightComboBox->setCurrentIndex (0);
61             break;
62         case QFont::Medium:
63             ui->weightComboBox->setCurrentIndex (1);
64             break;
65         case QFont::Bold:
66             ui->weightComboBox->setCurrentIndex (2);
67             break;
68         case QFont::Black:
69             ui->weightComboBox->setCurrentIndex (3);
70             break;
71         default:
72             if (weight < QFont::Normal)
73             {
74                 font_.setWeight (QFont::Normal);
75                 ui->weightComboBox->setCurrentIndex (0);
76             }
77             else if (weight < QFont::Medium)
78             {
79                 font_.setWeight (QFont::Medium);
80                 ui->weightComboBox->setCurrentIndex (1);
81             }
82             else if (weight < QFont::Bold)
83             {
84                 font_.setWeight (QFont::Bold);
85                 ui->weightComboBox->setCurrentIndex (2);
86             }
87             else
88             {
89                 font_.setWeight (QFont::Black);
90                 ui->weightComboBox->setCurrentIndex (3);
91             }
92             break;
93         }
94     }
95 
96     ui->lineEdit->setFont (font_);
97     ui->spinBox->setValue (font_.pointSize());
98 
99     connect (ui->fontComboBox, &QFontComboBox::currentFontChanged, [this] (const QFont &curFont) {
100         int fontSize = font_.pointSize();
101         int weight = font_.weight();
102         bool italic = font_.italic();
103         font_ = curFont;
104         font_.setPointSize (fontSize);
105         font_.setWeight (weight);
106         font_.setItalic (italic);
107         ui->lineEdit->setFont (font_);
108     });
109     connect (ui->monoFontComboBox, &QFontComboBox::currentFontChanged, [this] (const QFont &curFont) {
110         int fontSize = font_.pointSize();
111         font_ = curFont;
112         font_.setPointSize (fontSize);
113         ui->lineEdit->setFont (font_);
114     });
115 
116     connect (ui->spinBox, QOverload<int>::of(&QSpinBox::valueChanged), [this] (int value) {
117         font_.setPointSize (value);
118         ui->lineEdit->setFont (font_);
119     });
120 
121     connect (ui->codingFontBox, &QCheckBox::stateChanged, [this] (int checked) {
122         int fontSize = font_.pointSize();
123 
124         if (checked == Qt::Checked)
125         {
126             ui->fontComboBox->hide();
127             ui->fontLabel->hide();
128             ui->monoFontComboBox->show();
129             ui->monoFontLabel->show();
130 
131             font_ = ui->fontComboBox->currentFont();
132             if (!QFontInfo (font_).fixedPitch())
133                 font_ = ui->monoFontComboBox->currentFont();
134         }
135         else if (checked == Qt::Unchecked)
136         {
137             ui->monoFontComboBox->hide();
138             ui->monoFontLabel->hide();
139             ui->fontComboBox->show();
140             ui->fontLabel->show();
141 
142             font_ = ui->monoFontComboBox->currentFont();
143         }
144 
145         ui->italicBox->setChecked (false);
146         ui->weightComboBox->setCurrentIndex (0);
147 
148         font_.setPointSize (fontSize);
149         font_.setWeight (QFont::Normal);
150         font_.setItalic (false);
151         ui->monoFontComboBox->setCurrentFont (font_);
152         ui->fontComboBox->setCurrentFont (font_);
153         ui->lineEdit->setFont (font_);
154     });
155 
156     connect (ui->italicBox, &QCheckBox::stateChanged, [this] (int checked) {
157         if (checked == Qt::Checked)
158             font_.setItalic (true);
159         else if (checked == Qt::Unchecked)
160             font_.setItalic (false);
161         ui->lineEdit->setFont (font_);
162     });
163 
164     connect (ui->weightComboBox, QOverload<const int>::of(&QComboBox::currentIndexChanged), [this] (int index) {
165         switch (index) {
166         case 0:
167             font_.setWeight (QFont::Normal);
168             break;
169         case 1:
170             font_.setWeight (QFont::Medium);
171             break;
172         case 2:
173             font_.setWeight (QFont::Bold);
174             break;
175         case 3:
176             font_.setWeight (QFont::Black);
177             break;
178         default:
179             break;
180         }
181         ui->lineEdit->setFont (font_);
182     });
183 
184     resize (QSize (widthHint, sizeHint().height())); // show it compact
185 }
186 /*************************/
~FontDialog()187 FontDialog::~FontDialog()
188 {
189     delete ui; ui = nullptr;
190 }
191 
192 }
193