1 /* coded by Ketmar // Vampire Avalon (psyc://ketmar.no-ip.org/~Ketmar)
3 * This program is free software. It comes without any warranty, to
4 * the extent permitted by applicable law. You can redistribute it
5 * and/or modify it under the terms of the Do What The Fuck You Want
6 * To Public License, Version 2, as published by Sam Hocevar. See
7 * http://sam.zoy.org/wtfpl/COPYING for more details.
11 #include "selectwnd.h"
14 #include <QMessageBox>
17 #include <QPushButton>
21 #include "thememodel.h"
22 #include "itemdelegate.h"
29 SelectWnd::SelectWnd (QWidget
*parent
) : QWidget(parent
) {
32 mModel
= new XCursorThemeModel(this);
34 int size
= style()->pixelMetric(QStyle::PM_LargeIconSize
);
35 lbThemes
->setModel(mModel
);
36 lbThemes
->setItemDelegate(new ItemDelegate(this));
37 lbThemes
->setIconSize(QSize(size
, size
));
38 lbThemes
->setSelectionMode(QAbstractItemView::SingleSelection
);
40 // Make sure we find out about selection changes
41 connect(lbThemes
->selectionModel(), SIGNAL(currentChanged(const QModelIndex
&, const QModelIndex
&)), SLOT(currentChanged(const QModelIndex
&, const QModelIndex
&)));
43 // Disable the install button if we can't install new themes to ~/.icons,
44 // or Xcursor isn't set up to look for cursor themes there
45 if (!mModel
->searchPaths().contains(QDir::homePath() + "/.icons") || !iconsIsWritable()) btInstall
->setEnabled(false);
47 //QTimer::singleShot(0, this, SLOT(setCurrent()));
51 SelectWnd::~SelectWnd () {
55 void SelectWnd::setCurrent () {
56 lbThemes
->selectionModel()->clear();
58 QString ct
= getCurrentTheme();
59 mAppliedIndex
= mModel
->defaultIndex();
61 if (!ct
.isEmpty()) mAppliedIndex
= mModel
->findIndex(ct
);
62 else mAppliedIndex
= mModel
->defaultIndex();
64 if (mAppliedIndex
.isValid()) {
65 const XCursorThemeData
*theme
= mModel
->theme(mAppliedIndex
);
66 // Select the current theme
67 selectRow(mAppliedIndex
);
68 lbThemes
->scrollTo(mAppliedIndex
, QListView::PositionAtCenter
);
69 // Update the preview widget as well
70 if (theme
) preview
->setTheme(*theme
);// else preview->clearTheme();
75 bool SelectWnd::iconsIsWritable () const {
76 const QFileInfo icons
= QFileInfo(QDir::homePath() + "/.icons");
77 const QFileInfo home
= QFileInfo(QDir::homePath());
78 return ((icons
.exists() && icons
.isDir() && icons
.isWritable()) || (!icons
.exists() && home
.isWritable()));
82 void SelectWnd::keyPressEvent (QKeyEvent
*e
) {
83 if (e
->key() == Qt::Key_Escape
) close();
87 void SelectWnd::selectRow (int row
) const {
88 // Create a selection that stretches across all columns
89 QModelIndex from
= mModel
->index(row
, 0);
90 QModelIndex to
= mModel
->index(row
, mModel
->columnCount()-1);
91 QItemSelection
selection(from
, to
);
92 lbThemes
->selectionModel()->select(selection
, QItemSelectionModel::Select
);
93 lbThemes
->selectionModel()->setCurrentIndex(mAppliedIndex
, QItemSelectionModel::NoUpdate
);
97 void SelectWnd::currentChanged (const QModelIndex
¤t
, const QModelIndex
&previous
) {
99 if (current
.isValid()) {
100 const XCursorThemeData
*theme
= mModel
->theme(current
);
101 if (theme
) preview
->setTheme(*theme
); else preview
->clearTheme();
102 btRemove
->setEnabled(theme
->isWritable());
103 //qDebug() << theme->path() << theme->name();
104 } else preview
->clearTheme();
105 //emit changed(mAppliedIndex != current);
109 void SelectWnd::on_btInstall_clicked () {
110 qDebug() << "'install' clicked";
114 void SelectWnd::on_btSet_clicked () {
115 //qDebug() << "'set' clicked";
116 const XCursorThemeData
*theme
= mModel
->theme(lbThemes
->currentIndex());
119 fixXDefaults(theme
->name());
123 void SelectWnd::on_btRemove_clicked () {
124 qDebug() << "'remove' clicked";
125 const XCursorThemeData
*theme
= mModel
->theme(lbThemes
->currentIndex());
127 QString ct
= getCurrentTheme();
128 if (ct
== theme
->name()) {
129 QMessageBox::warning(this, tr("XCurTheme error"), tr("You can't remove active theme!"), QMessageBox::Ok
, QMessageBox::Ok
);
132 mModel
->removeTheme(lbThemes
->currentIndex());
133 QDir
d(theme
->path());
134 removeXCursorTheme(d
);