aboutsummaryrefslogtreecommitdiff
path: root/src-qt5/core/lumina-theme-engine/src/lthemeengine/qsspage.cpp
blob: 6377016cce0c6510866479b8233ecaa6edfa8cba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#include <QSettings>
#include <QDir>
#include <QInputDialog>
#include <QMessageBox>
#include <QFile>
#include <QMenu>
#include <QDebug>
#include <QTimer>

#include "lthemeengine.h"
#include "qsseditordialog.h"
#include "qsspage.h"
#include "ui_qsspage.h"

#define QSS_FULL_PATH_ROLE (Qt::ItemDataRole(Qt::UserRole))
#define QSS_WRITABLE_ROLE (Qt::ItemDataRole(Qt::UserRole + 1))

QSSPage::QSSPage(QWidget *parent, bool desktop) : TabPage(parent), m_ui(new Ui::QSSPage){
  m_ui->setupUi(this);
  desktop_qss = desktop;
  QDir("/").mkpath(lthemeengine::userStyleSheetPath());
  m_menu = new QMenu(this);
  m_menu->addAction(QIcon::fromTheme("accessories-text-editor"), tr("Edit"), this, SLOT(on_editButton_clicked()));
  m_menu->addAction(tr("Rename"), this, SLOT(on_renameButton_clicked()));
  m_menu->addSeparator();
  m_menu->addAction(QIcon::fromTheme("edit-delete"), tr("Remove"), this, SLOT(on_removeButton_clicked()));
  readSettings();
  //icons
  m_ui->createButton->setIcon(QIcon::fromTheme("document-new"));
  m_ui->editButton->setIcon(QIcon::fromTheme("accessories-text-editor"));
  m_ui->removeButton->setIcon(QIcon::fromTheme("edit-delete"));
  m_ui->tool_enable->setEnabled(false);
  m_ui->tool_disable->setEnabled(false);
  m_ui->copyButton->setEnabled(false);
}

QSSPage::~QSSPage(){
  delete m_ui;
}

void QSSPage::writeSettings(){
  QStringList styleSheets;
  QSettings settings(lthemeengine::configFile(), QSettings::IniFormat);
  for(int i = m_ui->qssListWidget->count()-1; i>=0; i--){
    QListWidgetItem *item = m_ui->qssListWidget->item(i);
    styleSheets << item->data(QSS_FULL_PATH_ROLE).toString();
  }
  if(desktop_qss){ settings.setValue("Interface/desktop_stylesheets", styleSheets); }
  else{ settings.setValue("Interface/stylesheets", styleSheets); }
}

void QSSPage::on_qssListWidget_currentItemChanged(QListWidgetItem *current, QListWidgetItem *){
  if(current!=0){
    m_ui->list_disabled->clearSelection(); //clear any current selection on the other widget
    m_ui->list_disabled->setCurrentRow(-1);
    m_ui->tool_enable->setEnabled(false);
  }
  //qDebug() << "Got Current Item Changed";
  m_ui->tool_disable->setEnabled(current!=0);
  m_ui->copyButton->setEnabled(current!=0);
  if(current){
    m_ui->editButton->setEnabled(current->data(QSS_WRITABLE_ROLE).toBool());
    m_ui->removeButton->setEnabled(current->data(QSS_WRITABLE_ROLE).toBool());
    m_ui->renameButton->setEnabled(current->data(QSS_WRITABLE_ROLE).toBool());
    }
  else{
    m_ui->editButton->setEnabled(false);
    m_ui->removeButton->setEnabled(false);
    m_ui->renameButton->setEnabled(false);
    }
}

void QSSPage::on_list_disabled_currentItemChanged(QListWidgetItem *current, QListWidgetItem *){
  if(current!=0){
    m_ui->qssListWidget->clearSelection(); //clear any current selection on the other widget
    m_ui->qssListWidget->setCurrentRow(-1);
    m_ui->tool_disable->setEnabled(false);
  }
  //qDebug() << "Got Current Item Changed";
  m_ui->tool_enable->setEnabled(current!=0);
  m_ui->copyButton->setEnabled(current!=0);
  if(current){
    m_ui->editButton->setEnabled(current->data(QSS_WRITABLE_ROLE).toBool());
    m_ui->removeButton->setEnabled(current->data(QSS_WRITABLE_ROLE).toBool());
    m_ui->renameButton->setEnabled(current->data(QSS_WRITABLE_ROLE).toBool());
    }
  else{
    m_ui->editButton->setEnabled(false);
    m_ui->removeButton->setEnabled(false);
    m_ui->renameButton->setEnabled(false);
    }
}

void QSSPage::on_createButton_clicked(){
  QString name = QInputDialog::getText(this, tr("Enter Style Sheet Name"), tr("File name:"));
  if(name.isEmpty()){ return; }
  if(!name.endsWith(".qss", Qt::CaseInsensitive)){ name.append(".qss"); }
  QString filePath;
    if(desktop_qss){ filePath = lthemeengine::userDesktopStyleSheetPath() + name; }
    else{ filePath = lthemeengine::userStyleSheetPath() + name; }
  if(QFile::exists(filePath)){
    QMessageBox::warning(this, tr("Error"), tr("The file \"%1\" already exists").arg(filePath));
    return;
    }
  // Make sure the directory exists
  QString dir = filePath.section("/",0,-2);
  if(!QFile::exists(dir)){
    QDir D(dir);
    D.mkpath(dir);
  }
  //creating empty file
  QFile file(filePath);
  file.open(QIODevice::WriteOnly);
  file.close();
  //creating item
  QFileInfo info(filePath);
  QListWidgetItem *item = new QListWidgetItem(info.fileName(),  m_ui->list_disabled);
  item->setToolTip(info.filePath());
  item->setData(QSS_FULL_PATH_ROLE, info.filePath());
  item->setData(QSS_WRITABLE_ROLE, info.isWritable());
  m_ui->list_disabled->setCurrentRow(m_ui->list_disabled->count()-1);
  QTimer::singleShot(10, this, SLOT(on_editButton_clicked()) );
}

void QSSPage::on_editButton_clicked(){
  QListWidgetItem *item = currentSelection();
  if(item){
    QSSEditorDialog dialog(item->data(QSS_FULL_PATH_ROLE).toString(), this);
    dialog.exec();
    }
}

void QSSPage::on_copyButton_clicked(){
 QListWidgetItem *sel = currentSelection();
  if(sel==0){ return; }
 QString name = QInputDialog::getText(this, tr("Enter Style Sheet Name"), tr("File name:"), QLineEdit::Normal, sel->text().section(".qss",0,0)+"_copy");
  if(name.isEmpty()){ return; }
  if(!name.endsWith(".qss", Qt::CaseInsensitive)){ name.append(".qss"); }
  QString filePath;
    if(desktop_qss){ filePath = lthemeengine::userDesktopStyleSheetPath() + name; }
    else{ filePath = lthemeengine::userStyleSheetPath() + name; }
  if(QFile::exists(filePath)){
    QMessageBox::warning(this, tr("Error"), tr("The file \"%1\" already exists").arg(filePath));
    return;
    }
  // Make sure the directory exists
  QString dir = filePath.section("/",0,-2);
  if(!QFile::exists(dir)){
    QDir D(dir);
    D.mkpath(dir);
  }
  //Copy the file over
  QFile::copy(sel->data(QSS_FULL_PATH_ROLE).toString(), filePath);
  //creating item
  QFileInfo info(filePath);
  QListWidgetItem *item = new QListWidgetItem(info.fileName(),  m_ui->list_disabled);
  item->setToolTip(info.filePath());
  item->setData(QSS_FULL_PATH_ROLE, info.filePath());
  item->setData(QSS_WRITABLE_ROLE, info.isWritable());
  m_ui->list_disabled->setCurrentRow(m_ui->list_disabled->count()-1);
}

void QSSPage::on_removeButton_clicked(){
  QListWidgetItem *item = currentSelection();
  if(!item){ return; }
  int button = QMessageBox::question(this, tr("Confirm Remove"),tr("Are you sure you want to remove style sheet \"%1\"?").arg(item->text()), QMessageBox::Yes | QMessageBox::No);
  if(button == QMessageBox::Yes){ QFile::remove(item->data(QSS_FULL_PATH_ROLE).toString()); }
  delete item;
}

void QSSPage::on_tool_enable_clicked(){
  QList<QListWidgetItem*> sel = m_ui->list_disabled->selectedItems();
  //qDebug() << "Got Selection:" << sel.count();
  for(int i=0; i<sel.length(); i++){
    m_ui->qssListWidget->addItem(sel[i]->clone());
    delete sel[i];
    //QCoreApplication::processEvents();
    m_ui->qssListWidget->setCurrentRow(m_ui->qssListWidget->count()-1);
  }

}

void QSSPage::on_tool_disable_clicked(){
  QList<QListWidgetItem*> sel = m_ui->qssListWidget->selectedItems();
  //qDebug() << "Got Selection:" << sel.count();
  for(int i=0; i<sel.length(); i++){
    m_ui->list_disabled->addItem(sel[i]->clone());
    delete sel[i];
    //QCoreApplication::processEvents();
    m_ui->list_disabled->setCurrentRow(m_ui->list_disabled->count()-1);
  }
}

void QSSPage::on_tool_priority_up_clicked(){
  QList<QListWidgetItem*> sel = m_ui->qssListWidget->selectedItems();
  for(int i=0; i<sel.length(); i++){
    int index = m_ui->qssListWidget->row(sel[i]);
    //qDebug() << "Move Item Up:" << index;
    if(index>0){
      m_ui->qssListWidget->insertItem(index-1, m_ui->qssListWidget->takeItem(index));
      m_ui->qssListWidget->setCurrentRow(index-1);
    }
  }
}

void QSSPage::on_tool_priority_down_clicked(){
  QList<QListWidgetItem*> sel = m_ui->qssListWidget->selectedItems();
  for(int i=0; i<sel.length(); i++){
    int index = m_ui->qssListWidget->row(sel[i]);
    //qDebug() << "Move Item Down:" << index;
    if(index<(m_ui->qssListWidget->count()-1) ){
      m_ui->qssListWidget->insertItem(index+1, m_ui->qssListWidget->takeItem(index));
      m_ui->qssListWidget->setCurrentRow(index+1);
    }
  }
}

void QSSPage::readSettings(){
  //load stylesheets
  m_ui->qssListWidget->clear();
  m_ui->list_disabled->clear();
  //Read the currently-enabled settings
  QSettings settings(lthemeengine::configFile(), QSettings::IniFormat);
  QStringList styleSheets;
  if(desktop_qss){ styleSheets = settings.value("Interface/desktop_stylesheets").toStringList(); }
  else{ styleSheets = settings.value("Interface/stylesheets").toStringList(); }
  //Now load the items into list widgets
  if(desktop_qss){ findStyleSheets(QStringList() << lthemeengine::userDesktopStyleSheetPath() << lthemeengine::sharedDesktopStyleSheetPath(), styleSheets); }
  else{findStyleSheets(QStringList() << lthemeengine::userStyleSheetPath() << lthemeengine::sharedStyleSheetPath(), styleSheets); }

}

void QSSPage::findStyleSheets(QStringList paths, QStringList enabled){
  paths.removeDuplicates();
  for(int i=0; i<paths.length(); i++){
    if(!QFile::exists(paths[i])){ continue; }
    QDir dir(paths[i]);
    dir.setFilter(QDir::Files);
    dir.setNameFilters(QStringList() << "*.qss");
    foreach (QFileInfo info, dir.entryInfoList()){
      QListWidgetItem *item = new QListWidgetItem(info.fileName());
      item->setToolTip(info.filePath());
      item->setData(QSS_FULL_PATH_ROLE, info.filePath());
      item->setData(QSS_WRITABLE_ROLE, info.isWritable());
      if( enabled.contains(info.filePath()) ){ m_ui->qssListWidget->addItem(item); }
      else{ m_ui->list_disabled->addItem(item); }
    }
  }
  //Now ensure the priority of the items in the active list is correct
  for(int i = 0; i < m_ui->qssListWidget->count(); ++i){
    QListWidgetItem *item = m_ui->qssListWidget->item(i);
    int index = enabled.indexOf( item->data(QSS_FULL_PATH_ROLE).toString() );
    if(index>=0){ m_ui->qssListWidget->insertItem(index, item); }// item->move(m_ui->qssListWidget->count() - 1 - index); }
  }
  m_ui->list_disabled->sortItems(Qt::AscendingOrder);

}

void QSSPage::on_renameButton_clicked(){
  QListWidgetItem *item = currentSelection();
  if(!item){ return; }
  QString name = QInputDialog::getText(this, tr("Rename Style Sheet"), tr("Style sheet name:"), QLineEdit::Normal, item->text(), 0);
  if(name.isEmpty()){ return; }
  if(!m_ui->qssListWidget->findItems(name, Qt::MatchExactly).isEmpty() || !m_ui->list_disabled->findItems(name, Qt::MatchExactly).isEmpty()){
    QMessageBox::warning(this, tr("Error"), tr("The style sheet \"%1\" already exists").arg(name));
    return;
    }
  if(!name.endsWith(".qss", Qt::CaseInsensitive)){ name.append(".qss"); }
  QString newPath = lthemeengine::userStyleSheetPath() + name;
  if(!QFile::rename(item->data(QSS_FULL_PATH_ROLE).toString(), newPath)){
    QMessageBox::warning(this, tr("Error"), tr("Unable to rename file"));
    return;
    }
  item->setText(name);
  item->setData(QSS_FULL_PATH_ROLE, newPath);
  item->setToolTip(newPath);
}

void QSSPage::on_qssListWidget_customContextMenuRequested(const QPoint &pos){
  QListWidgetItem *item = m_ui->qssListWidget->currentItem();
  if(item && item->data(QSS_WRITABLE_ROLE).toBool()){ m_menu->exec(m_ui->qssListWidget->viewport()->mapToGlobal(pos)); }
}

QListWidgetItem* QSSPage::currentSelection(){
  QListWidgetItem *item = m_ui->qssListWidget->currentItem();
  if(item==0){ item = m_ui->list_disabled->currentItem(); }
  return item;
}
bgstack15