1 /************************************************************************
2  **
3  **  Copyright (C) 2020-2021 Kevin B. Hendricks
4  **
5  **  This file is part of Sigil.
6  **
7  **  Sigil is free software: you can redistribute it and/or modify
8  **  it under the terms of the GNU General Public License as published by
9  **  the Free Software Foundation, either version 3 of the License, or
10  **  (at your option) any later version.
11  **
12  **  Sigil is distributed in the hope that it will be useful,
13  **  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  **  GNU General Public License for more details.
16  **
17  **  You should have received a copy of the GNU General Public License
18  **  along with Sigil.  If not, see <http://www.gnu.org/licenses/>.
19  **
20  *************************************************************************/
21 
22 #include <QString>
23 #include <QFileInfo>
24 #include <QApplication>
25 #include <QGuiApplication>
26 #include <QVBoxLayout>
27 #include <QHBoxLayout>
28 #include <QGuiApplication>
29 #include <QApplication>
30 #include "MainUI/MainApplication.h"
31 #include "Misc/Utility.h"
32 #include "Misc/SettingsStore.h"
33 #include "Widgets/FontView.h"
34 #include "Dialogs/ViewFont.h"
35 
36 static QString SETTINGS_GROUP = "view_font";
37 
ViewFont(QWidget * parent)38 ViewFont::ViewFont(QWidget *parent)
39     : QDialog(parent),
40       m_fv(new FontView(this)),
41       m_bp(new QToolButton(this)),
42       m_layout(new QVBoxLayout(this))
43 {
44     m_layout->addWidget(m_fv);
45     m_bp->setToolTip(tr("Close this window"));
46     m_bp->setText(tr("Done"));
47     m_bp->setToolButtonStyle(Qt::ToolButtonTextOnly);
48     QHBoxLayout* hl = new QHBoxLayout();
49     hl->addStretch(0);
50     hl->addWidget(m_bp);
51     m_layout->addLayout(hl);
52     ReadSettings();
53     ConnectSignalsToSlots();
54 }
55 
~ViewFont()56 ViewFont::~ViewFont()
57 {
58     WriteSettings();
59 }
60 
61 
sizeHint()62 QSize ViewFont::sizeHint()
63 {
64     return QSize(450,250);
65 }
66 
ShowFont(QString path)67 void ViewFont::ShowFont(QString path)
68 {
69     m_fv->ShowFont(path);
70     QApplication::processEvents();
71 }
72 
ReloadViewer()73 void ViewFont::ReloadViewer()
74 {
75     m_fv->ReloadViewer();
76 }
77 
ReadSettings()78 void ViewFont::ReadSettings()
79 {
80     SettingsStore settings;
81     settings.beginGroup(SETTINGS_GROUP);
82     QByteArray geometry = settings.value("geometry").toByteArray();
83     if (!geometry.isNull()) {
84         restoreGeometry(geometry);
85     } else {
86         resize(sizeHint());
87     }
88     settings.endGroup();
89 }
90 
WriteSettings()91 void ViewFont::WriteSettings()
92 {
93     SettingsStore settings;
94     settings.beginGroup(SETTINGS_GROUP);
95     settings.setValue("geometry", saveGeometry());
96     settings.endGroup();
97 }
98 
ConnectSignalsToSlots()99 void ViewFont::ConnectSignalsToSlots()
100 {
101 #ifdef Q_OS_MAC
102     MainApplication *mainApplication = qobject_cast<MainApplication *>(qApp);
103     connect(mainApplication, SIGNAL(applicationPaletteChanged()), this, SLOT(ReloadViewer()));
104 #endif
105     connect(m_bp, SIGNAL(clicked()), this, SLOT(accept()));
106 }
107