1 /************************************************************************
2 **
3 **  Copyright (C) 2021 Kevin B. Hendricks, Stratford, Ontario Canada
4 **  Copyright (C) 2012 Dave Heiland
5 **  Copyright (C) 2012 John Schember <john@nachtimwald.com>
6 **
7 **  This file is part of Sigil.
8 **
9 **  Sigil is free software: you can redistribute it and/or modify
10 **  it under the terms of the GNU General Public License as published by
11 **  the Free Software Foundation, either version 3 of the License, or
12 **  (at your option) any later version.
13 **
14 **  Sigil is distributed in the hope that it will be useful,
15 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 **  GNU General Public License for more details.
18 **
19 **  You should have received a copy of the GNU General Public License
20 **  along with Sigil.  If not, see <http://www.gnu.org/licenses/>.
21 **
22 *************************************************************************/
23 
24 #include <QtGui/QStandardItem>
25 
26 #include "Dialogs/LinkJavascripts.h"
27 #include "Misc/SettingsStore.h"
28 #include "sigil_constants.h"
29 
30 static const QString SETTINGS_GROUP   = "link_javascripts";
31 
32 // Constructor;
LinkJavascripts(QList<std::pair<QString,bool>> javascripts_map,QWidget * parent)33 LinkJavascripts::LinkJavascripts(QList<std::pair<QString, bool>> javascripts_map, QWidget *parent)
34     :
35     QDialog(parent),
36     m_JavascriptsMap(javascripts_map)
37 {
38     ui.setupUi(this);
39     ConnectSignalsToSlots();
40     ui.JavascriptsView->setModel(&m_JavascriptsModel);
41     CreateJavascriptsModel();
42     UpdateTreeViewDisplay();
43     ReadSettings();
44 }
45 
46 
47 // Updates the display of the tree view (resize columns)
UpdateTreeViewDisplay()48 void LinkJavascripts::UpdateTreeViewDisplay()
49 {
50     ui.JavascriptsView->expandAll();
51     ui.JavascriptsView->resizeColumnToContents(0);
52     ui.JavascriptsView->setColumnWidth(0, ui.JavascriptsView->columnWidth(0));
53     ui.JavascriptsView->setCurrentIndex(m_JavascriptsModel.index(0, 0));
54 }
55 
56 // Creates the model that is displayed in the tree view
CreateJavascriptsModel()57 void LinkJavascripts::CreateJavascriptsModel()
58 {
59     m_JavascriptsModel.clear();
60     QStringList header;
61     header.append(tr("Include"));
62     header.append(tr("Javascript"));
63     m_JavascriptsModel.setHorizontalHeaderLabels(header);
64 
65     // Inserts all entries
66     for (int i = 0; i < m_JavascriptsMap.count(); i++) {
67         InsertJavascriptIntoModel(m_JavascriptsMap.at(i));
68     }
69 }
70 
71 
72 // Inserts the specified heading into the model
InsertJavascriptIntoModel(std::pair<QString,bool> javascript_pair)73 void LinkJavascripts::InsertJavascriptIntoModel(std::pair<QString, bool> javascript_pair)
74 {
75     QStandardItem *item_bookpath       = new QStandardItem(javascript_pair.first);
76     QStandardItem *item_included_check = new QStandardItem();
77     item_included_check->setEditable(false);
78     item_included_check->setCheckable(true);
79     item_bookpath->setEditable(false);
80     item_bookpath->setDragEnabled(false);
81     item_bookpath->setDropEnabled(false);
82 
83     if (javascript_pair.second) {
84         item_included_check->setCheckState(Qt::Checked);
85     } else {
86         item_included_check->setCheckState(Qt::Unchecked);
87     }
88 
89     QList<QStandardItem *> items;
90     items << item_included_check << item_bookpath;
91     m_JavascriptsModel.invisibleRootItem()->appendRow(items);
92 }
93 
94 
95 // Reads all the stored dialog settings like window position, size, etc.
ReadSettings()96 void LinkJavascripts::ReadSettings()
97 {
98     SettingsStore settings;
99     settings.beginGroup(SETTINGS_GROUP);
100     // The size of the window and it's full screen status
101     QByteArray geometry = settings.value("geometry").toByteArray();
102 
103     if (!geometry.isNull()) {
104         restoreGeometry(geometry);
105     }
106 
107     settings.endGroup();
108 }
109 
110 
111 // Writes all the stored dialog settings like window position, size, etc.
WriteSettings()112 void LinkJavascripts::WriteSettings()
113 {
114     SettingsStore settings;
115     settings.beginGroup(SETTINGS_GROUP);
116     // The size of the window and it's full screen status
117     settings.setValue("geometry", saveGeometry());
118     settings.endGroup();
119 }
120 
MoveUp()121 void LinkJavascripts::MoveUp()
122 {
123     QModelIndexList selected_indexes = ui.JavascriptsView->selectionModel()->selectedIndexes();
124 
125     if (selected_indexes.isEmpty()) {
126         return;
127     }
128 
129     QModelIndex index = selected_indexes.first();
130     int row = index.row();
131 
132     if (row == 0) {
133         return;
134     }
135 
136     QList<QStandardItem *> items =  m_JavascriptsModel.invisibleRootItem()->takeRow(row - 1);
137     m_JavascriptsModel.invisibleRootItem()->insertRow(row, items);
138 }
139 
MoveDown()140 void LinkJavascripts::MoveDown()
141 {
142     QModelIndexList selected_indexes = ui.JavascriptsView->selectionModel()->selectedIndexes();
143 
144     if (selected_indexes.isEmpty()) {
145         return;
146     }
147 
148     QModelIndex index = selected_indexes.first();
149     int row = index.row();
150 
151     if (row == m_JavascriptsModel.invisibleRootItem()->rowCount() - 1) {
152         return;
153     }
154 
155     QList<QStandardItem *> items =  m_JavascriptsModel.invisibleRootItem()->takeRow(row + 1);
156     m_JavascriptsModel.invisibleRootItem()->insertRow(row, items);
157 }
158 
159 
UpdateJavascripts()160 void LinkJavascripts::UpdateJavascripts()
161 {
162     m_Javascripts.clear();
163     int rows = m_JavascriptsModel.invisibleRootItem()->rowCount();
164 
165     for (int row = 0; row < rows; row++) {
166         QList<QStandardItem *> items =  m_JavascriptsModel.invisibleRootItem()->takeRow(0);
167 
168         if (items.at(0)->checkState() == Qt::Checked) {
169             m_Javascripts << items.at(1)->data(Qt::DisplayRole).toString();
170         }
171     }
172 }
173 
GetJavascripts()174 QStringList LinkJavascripts::GetJavascripts()
175 {
176     return m_Javascripts;
177 }
178 
ConnectSignalsToSlots()179 void LinkJavascripts::ConnectSignalsToSlots()
180 {
181     connect(ui.MoveUp, SIGNAL(clicked()),  this, SLOT(MoveUp()));
182     connect(ui.MoveDown, SIGNAL(clicked()),  this, SLOT(MoveDown()));
183     connect(this, SIGNAL(accepted()), this, SLOT(UpdateJavascripts()));
184     connect(this, SIGNAL(accepted()), this, SLOT(WriteSettings()));
185 }
186