1 /************************************************************************
2 **
3 **  Copyright (C) 2015-2021 Kevin B. Hendricks, Stratford Ontario Canada
4 **  Copyright (C) 2012      Dave Heiland
5 **
6 **  This file is part of Sigil.
7 **
8 **  Sigil is free software: you can redistribute it and/or modify
9 **  it under the terms of the GNU General Public License as published by
10 **  the Free Software Foundation, either version 3 of the License, or
11 **  (at your option) any later version.
12 **
13 **  Sigil is distributed in the hope that it will be useful,
14 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
15 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 **  GNU General Public License for more details.
17 **
18 **  You should have received a copy of the GNU General Public License
19 **  along with Sigil.  If not, see <http://www.gnu.org/licenses/>.
20 **
21 *************************************************************************/
22 
23 #include <QtWidgets/QPushButton>
24 #include "Misc/SettingsStore.h"
25 #include "Dialogs/DeleteStyles.h"
26 
27 static const QString SETTINGS_GROUP      = "delete_styles";
28 
DeleteStyles(QHash<QString,QList<CSSInfo::CSSSelector * >> css_styles_to_delete,QWidget * parent)29 DeleteStyles::DeleteStyles(QHash<QString, QList<CSSInfo::CSSSelector *>> css_styles_to_delete, QWidget *parent)
30     :
31     QDialog(parent),
32     m_CSSStylesToDelete(css_styles_to_delete)
33 {
34     ui.setupUi(this);
35     ConnectSignals();
36     SetUpTable();
37     ReadSettings();
38     // Get list of styles
39     QHashIterator<QString, QList<CSSInfo::CSSSelector *>> stylesheets(m_CSSStylesToDelete);
40 
41     while (stylesheets.hasNext()) {
42         stylesheets.next();
43         // css_filename has been converted to a book path
44         QString css_filename = stylesheets.key();
45         foreach(CSSInfo::CSSSelector * s, stylesheets.value()) {
46             QList<QStandardItem *> rowItems;
47             // Checkbox
48             QStandardItem *checkbox_item = new QStandardItem();
49             checkbox_item->setCheckable(true);
50             checkbox_item->setCheckState(Qt::Checked);
51             rowItems << checkbox_item;
52             // Filename
53             QStandardItem *file_item = new QStandardItem();
54             file_item->setText(css_filename);
55             file_item->setData(QString::number(s->pos));
56             rowItems << file_item;
57             // Class
58             QStandardItem *class_item = new QStandardItem();
59             class_item->setText(s->text);
60             rowItems << class_item;
61 
62             for (int i = 0; i < rowItems.count(); i++) {
63                 rowItems[i]->setEditable(false);
64             }
65 
66             m_Model.appendRow(rowItems);
67         }
68     }
69 }
70 
~DeleteStyles()71 DeleteStyles::~DeleteStyles()
72 {
73     WriteSettings();
74 }
75 
SetUpTable()76 void DeleteStyles::SetUpTable()
77 {
78     QStringList header;
79     QPushButton *delete_button = ui.buttonBox->button(QDialogButtonBox::Ok);
80     delete_button->setText(tr("Delete Marked Styles"));
81     header.append(tr("Delete"));
82     header.append(tr("File"));
83     header.append(tr("Style"));
84     m_Model.setHorizontalHeaderLabels(header);
85     ui.Table->setModel(&m_Model);
86     // Make the header fill all the available space
87     ui.Table->horizontalHeader()->setStretchLastSection(true);
88     ui.Table->verticalHeader()->setVisible(false);
89     ui.Table->setSortingEnabled(true);
90     ui.Table->setSelectionBehavior(QAbstractItemView::SelectRows);
91     ui.Table->setSelectionMode(QAbstractItemView::SingleSelection);
92     ui.Table->setAlternatingRowColors(true);
93 }
94 
SaveStylesToDelete()95 void DeleteStyles::SaveStylesToDelete()
96 {
97     m_CSSStylesToDelete.clear();
98 
99     for (int row = 0; row < m_Model.rowCount(); row++) {
100         bool checked = m_Model.itemFromIndex(m_Model.index(row, 0))->checkState() == Qt::Checked;
101 
102         if (checked) {
103             QString filename  = m_Model.item(row, 1)->text();
104             int style_pos = m_Model.item(row, 1)->data().toInt();
105             QString style_name = m_Model.item(row, 2)->text();
106             CSSInfo::CSSSelector *selector = new CSSInfo::CSSSelector();
107             selector->text = style_name;
108             selector->pos = style_pos;
109             m_CSSStylesToDelete[filename].append(selector);
110         }
111     }
112 }
113 
GetStylesToDelete()114 QHash<QString, QList<CSSInfo::CSSSelector *>> DeleteStyles::GetStylesToDelete()
115 {
116     return m_CSSStylesToDelete;
117 }
118 
ReadSettings()119 void DeleteStyles::ReadSettings()
120 {
121     SettingsStore settings;
122     settings.beginGroup(SETTINGS_GROUP);
123     // The size of the window and it's full screen status
124     QByteArray geometry = settings.value("geometry").toByteArray();
125 
126     if (!geometry.isNull()) {
127         restoreGeometry(geometry);
128     }
129 
130     settings.endGroup();
131 }
132 
133 
WriteSettings()134 void DeleteStyles::WriteSettings()
135 {
136     SettingsStore settings;
137     settings.beginGroup(SETTINGS_GROUP);
138     // The size of the window and it's full screen status
139     settings.setValue("geometry", saveGeometry());
140     settings.endGroup();
141 }
142 
DoubleClick(const QModelIndex index)143 void DeleteStyles::DoubleClick(const QModelIndex index)
144 {
145     QString filename = m_Model.item(index.row(), 1)->text();
146     int position = m_Model.item(index.row(), 1)->data().toInt();
147     emit OpenFileRequest(filename, -1, position);
148 }
149 
ConnectSignals()150 void DeleteStyles::ConnectSignals()
151 {
152     connect(this, SIGNAL(accepted()), this, SLOT(SaveStylesToDelete()));
153     connect(ui.Table, SIGNAL(doubleClicked(const QModelIndex &)), this, SLOT(DoubleClick(const QModelIndex &)));
154 }
155