Advertisement
Guest User

psi+ themes patch

a guest
Apr 28th, 2017
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 16.13 KB | None | 0 0
  1. diff --git a/src/options/opt_theme.cpp b/src/options/opt_theme.cpp
  2. index 173b4473..f8b094a6 100644
  3. --- a/src/options/opt_theme.cpp
  4. +++ b/src/options/opt_theme.cpp
  5. @@ -5,6 +5,14 @@
  6.  #include "psithememodel.h"
  7.  #include "psithemeviewdelegate.h"
  8.  #include "psithememanager.h"
  9. +#include "psiiconset.h"
  10. +
  11. +#include <QRadioButton>
  12. +#include <QToolButton>
  13. +#include <QDialog>
  14. +
  15. +#define RADIO_PREFIX "rb_"
  16. +#define SCREEN_PREFIX "scb_"
  17.  
  18.  class OptAppearanceThemeUI : public QWidget, public Ui::OptAppearanceTheme
  19.  {
  20. @@ -32,7 +40,7 @@ OptionsTabAppearanceThemes::OptionsTabAppearanceThemes(QObject *parent)
  21.  //----------------------------------------------------------------------------
  22.  
  23.  OptionsTabAppearanceTheme::OptionsTabAppearanceTheme(QObject *parent,
  24. -                                                   PsiThemeProvider *provider_)
  25. +                            PsiThemeProvider *provider_)
  26.     : OptionsTab(parent, provider_->type(), "",
  27.                  provider_->optionsName(),
  28.                  provider_->optionsDescription())
  29. @@ -44,7 +52,8 @@ OptionsTabAppearanceTheme::OptionsTabAppearanceTheme(QObject *parent,
  30.  
  31.  OptionsTabAppearanceTheme::~OptionsTabAppearanceTheme()
  32.  {
  33. -
  34. +   if(screenshotDialog)
  35. +       delete screenshotDialog;
  36.  }
  37.  
  38.  QWidget *OptionsTabAppearanceTheme::widget()
  39. @@ -56,20 +65,16 @@ QWidget *OptionsTabAppearanceTheme::widget()
  40.     OptAppearanceThemeUI *d = (OptAppearanceThemeUI *)w;
  41.     themesModel = new PsiThemeModel(this);
  42.     themesModel->setType(provider->type());
  43. -   PsiThemeViewDelegate *vd = new PsiThemeViewDelegate(d->themeView);
  44. -   d->themeView->setItemDelegate(vd);
  45.     d->themeView->setModel(themesModel);
  46. -   int sw = provider->screenshotWidth();
  47. -   if (sw) {
  48. -       d->themeView->setFixedWidth(sw);
  49. -   }
  50.     connect(d->themeView->selectionModel(),
  51. -           SIGNAL(currentChanged(QModelIndex, QModelIndex)),
  52. -           SIGNAL(dataChanged()));
  53. -
  54. +       SIGNAL(currentChanged(QModelIndex, QModelIndex)),
  55. +       SIGNAL(dataChanged()));
  56. +   connect(d->themeView->selectionModel(),
  57. +       SIGNAL(currentChanged(QModelIndex,QModelIndex)),
  58. +       SLOT(itemChanged(QModelIndex,QModelIndex))); //changes radiobuttons state
  59.     connect(themesModel,
  60. -           SIGNAL(rowsInserted(QModelIndex,int,int)),
  61. -           SLOT(modelRowsInserted(QModelIndex,int,int)));
  62. +       SIGNAL(rowsInserted(QModelIndex,int,int)),
  63. +       SLOT(modelRowsInserted(QModelIndex,int,int)));
  64.  
  65.     return w;
  66.  }
  67. @@ -79,25 +84,154 @@ void OptionsTabAppearanceTheme::modelRowsInserted(const QModelIndex &parent, int
  68.     if (!parent.isValid() || !w) {
  69.         Theme *theme = provider->current();
  70.         OptAppearanceThemeUI *d = (OptAppearanceThemeUI *)w;
  71. +       const QSize buttonSize = QSize(21,21);
  72.         if (theme) {
  73.             for (int i = first; i <= last; i++) {
  74. -               QString id = themesModel->data(themesModel->index(i), PsiThemeModel::IdRole).toString();
  75. +               const QModelIndex index = themesModel->index(i);
  76. +               const QString id = themesModel->data(index, PsiThemeModel::IdRole).toString();
  77.                 if (id == theme->id()) {
  78. -                   d->themeView->setCurrentIndex(themesModel->index(i));
  79. -                   break;
  80. +                   d->themeView->setCurrentIndex(index);
  81.                 }
  82. +               const QString themeName = themesModel->data(index, PsiThemeModel::TitleRole).toString();
  83. +               bool enabled = (id == provider->current()->id());
  84. +               bool isPsi = id.startsWith("psi");
  85. +               const QPixmap client = isPsi ? IconsetFactory::iconPixmap("clients/psi")
  86. +                                : IconsetFactory::iconPixmap("clients/adium");
  87. +               const QString clientName = isPsi ? tr("Psi Theme") : tr("Adium Theme");
  88. +               QToolButton *screenshotButton = new QToolButton(d->themeView);
  89. +               screenshotButton->setIcon(QIcon(IconsetFactory::iconPixmap("psi/eye")));
  90. +               screenshotButton->resize(buttonSize);
  91. +               screenshotButton->setObjectName(SCREEN_PREFIX + id);
  92. +               screenshotButton->setToolTip(tr("Show theme screenshot"));
  93. +               connect(screenshotButton, SIGNAL(clicked()), this, SLOT(showThemeScreenshot()));
  94. +
  95. +               QLabel *typeLabel = new QLabel(d->themeView);
  96. +               typeLabel->setPixmap(client);
  97. +               typeLabel->setToolTip(clientName);
  98. +
  99. +               QRadioButton *status = new QRadioButton(d->themeView);
  100. +               status->setObjectName(RADIO_PREFIX + id);
  101. +               status->setChecked(enabled);
  102. +               status->setAutoExclusive(false);
  103. +               status->setText(themeName);
  104. +               connect(status, SIGNAL(toggled(bool)), this, SLOT(radioToggled(bool)));
  105. +
  106. +               QWidget *itemWidget = new QWidget(d->themeView);
  107. +
  108. +               QBoxLayout *box = new QBoxLayout(QBoxLayout::LeftToRight, d->themeView);
  109. +               box->addWidget(status);
  110. +               box->addStretch();
  111. +               box->addWidget(typeLabel);
  112. +               box->addWidget(screenshotButton);
  113. +               itemWidget->setLayout(box);
  114. +
  115. +               d->themeView->setIndexWidget(index, itemWidget);
  116. +           }
  117. +           d->themeView->sortByColumn(0);
  118. +       }
  119. +   }
  120. +}
  121. +
  122. +void OptionsTabAppearanceTheme::showThemeScreenshot()
  123. +{
  124. +   if ( !w || !sender()->inherits("QToolButton") )
  125. +       return;
  126. +   OptAppearanceThemeUI *d = (OptAppearanceThemeUI *)w;
  127. +   QToolButton *btn = static_cast<QToolButton*>(sender());
  128. +   if ( btn ) {
  129. +       if ( screenshotDialog )
  130. +           delete(screenshotDialog);
  131. +
  132. +       const QSize minSize(300, 100);
  133. +       screenshotDialog = new QDialog(d);
  134. +       screenshotDialog->setMinimumSize(minSize);
  135. +
  136. +       const int row = themesModel->themeRow(getThemeId(btn->objectName()));
  137. +       const QModelIndex index = themesModel->index(row);
  138. +       const QString name_ = themesModel->data(index, PsiThemeModel::TitleRole).toString();
  139. +       const QPixmap scr = themesModel->data(index, PsiThemeModel::ScreenshotRole).value<QPixmap>();
  140. +
  141. +       screenshotDialog->setWindowTitle(tr("%1 Screenshot").arg(name_));
  142. +       screenshotDialog->setWindowIcon(QIcon(IconsetFactory::iconPixmap("psi/logo_128")));
  143. +
  144. +       QBoxLayout *box = new QBoxLayout(QBoxLayout::LeftToRight, screenshotDialog);
  145. +       QLabel *image = new QLabel(screenshotDialog);
  146. +       if (!scr.isNull()) {
  147. +           image->setPixmap(scr);
  148. +       }
  149. +       else {
  150. +           image->setText(tr("No Image"));
  151. +       }
  152. +       box->addWidget(image);
  153. +
  154. +       screenshotDialog->setAttribute(Qt::WA_DeleteOnClose);
  155. +       screenshotDialog->show();
  156. +   }
  157. +}
  158. +
  159. +void OptionsTabAppearanceTheme::radioToggled(bool toggled)
  160. +{
  161. +   if ( !w  || !sender()->inherits("QRadioButton"))
  162. +       return;
  163. +
  164. +   OptAppearanceThemeUI *d = (OptAppearanceThemeUI *)w;
  165. +   QRadioButton *btn = static_cast<QRadioButton*>(sender());
  166. +   const QString btnObjectName = btn->objectName();
  167. +   if (btn && toggled) {
  168. +       const QModelIndex index = themesModel->index(themesModel->themeRow(getThemeId(btnObjectName)));
  169. +       if (index != d->themeView->currentIndex()) {
  170. +           d->themeView->setCurrentIndex(index);
  171. +       }
  172. +   }
  173. +   uncheckAll(btnObjectName);
  174. +}
  175. +
  176. +void OptionsTabAppearanceTheme::uncheckAll(const QString &name_)
  177. +{
  178. +   if ( !w )
  179. +       return;
  180. +
  181. +   OptAppearanceThemeUI *d = (OptAppearanceThemeUI *)w;
  182. +   const QList<QRadioButton*> buttons = d->themeView->findChildren<QRadioButton*>(QRegExp(RADIO_PREFIX"*"));
  183. +   if (!buttons.isEmpty()) {
  184. +       foreach (QRadioButton *b, buttons) {
  185. +           if (b->objectName() != name_) {
  186. +               b->setChecked(false);
  187.             }
  188.         }
  189.     }
  190.  }
  191.  
  192. +void OptionsTabAppearanceTheme::itemChanged(const QModelIndex &current, const QModelIndex &previous)
  193. +{
  194. +   if ( !w )
  195. +       return;
  196. +
  197. +   Q_UNUSED(previous)
  198. +   OptAppearanceThemeUI *d = (OptAppearanceThemeUI *)w;
  199. +   const QString id = themesModel->data(current, PsiThemeModel::IdRole).toString();
  200. +   if (!id.isEmpty()) {
  201. +       QRadioButton *btn = d->themeView->findChild<QRadioButton*>(QString("%1%2").arg(RADIO_PREFIX).arg(id));
  202. +       if (btn) {
  203. +           uncheckAll(btn->objectName());
  204. +           btn->setChecked(!btn->isChecked());
  205. +       }
  206. +   }
  207. +}
  208. +
  209. +QString OptionsTabAppearanceTheme::getThemeId(const QString &objName) const
  210. +{
  211. +   const int index = objName.indexOf("_", 0);
  212. +   return (index >0 ? objName.right(objName.length() - index - 1) : QString());
  213. +}
  214. +
  215.  void OptionsTabAppearanceTheme::applyOptions()
  216.  {
  217.     if ( !w )
  218.         return;
  219.  
  220.     OptAppearanceThemeUI *d = (OptAppearanceThemeUI *)w;
  221. -   QString id = d->themeView->currentIndex().data(PsiThemeModel::IdRole).toString();
  222. +   const QString id = d->themeView->currentIndex().data(PsiThemeModel::IdRole).toString();
  223.     if (!id.isEmpty()) {
  224.         provider->setCurrentTheme(id);
  225.     }
  226. diff --git a/src/options/opt_theme.h b/src/options/opt_theme.h
  227. index b9916110..c3a248ca 100644
  228. --- a/src/options/opt_theme.h
  229. +++ b/src/options/opt_theme.h
  230. @@ -2,11 +2,13 @@
  231.  #define OPT_THEME_H
  232.  
  233.  #include "optionstab.h"
  234. +#include <QPointer>
  235.  
  236.  class QModelIndex;
  237.  class QWidget;
  238.  class PsiThemeModel;
  239.  class PsiThemeProvider;
  240. +class QDialog;
  241.  
  242.  class OptionsTabAppearanceThemes : public MetaOptionsTab
  243.  {
  244. @@ -29,11 +31,19 @@ public:
  245.  
  246.  protected slots:
  247.     void modelRowsInserted(const QModelIndex &parent, int first, int last);
  248. +   void itemChanged(const QModelIndex &current, const QModelIndex &previous);
  249. +   void showThemeScreenshot();
  250. +   void radioToggled(bool toggled);
  251. +
  252. +private:
  253. +   QString getThemeId(const QString &objName) const;
  254. +   void uncheckAll(const QString &name_);
  255.  
  256.  private:
  257.     QWidget *w;
  258.     PsiThemeModel *themesModel;
  259.     PsiThemeProvider *provider;
  260. +   QPointer<QDialog> screenshotDialog;
  261.  };
  262.  
  263.  #endif
  264. diff --git a/src/options/opt_theme.ui b/src/options/opt_theme.ui
  265. index e526e20d..75f3750c 100644
  266. --- a/src/options/opt_theme.ui
  267. +++ b/src/options/opt_theme.ui
  268. @@ -17,20 +17,26 @@
  269.     <item>
  270.      <layout class="QHBoxLayout" name="adapterLayout">
  271.       <item>
  272. -      <widget class="QListView" name="themeView"/>
  273. -     </item>
  274. -     <item>
  275. -      <spacer name="horizontalSpacer_2">
  276. -       <property name="orientation">
  277. -        <enum>Qt::Horizontal</enum>
  278. +      <widget class="QTreeView" name="themeView">
  279. +       <property name="editTriggers">
  280. +        <set>QAbstractItemView::NoEditTriggers</set>
  281.         </property>
  282. -       <property name="sizeHint" stdset="0">
  283. -        <size>
  284. -         <width>40</width>
  285. -         <height>20</height>
  286. -        </size>
  287. +       <property name="showDropIndicator" stdset="0">
  288. +        <bool>false</bool>
  289.         </property>
  290. -      </spacer>
  291. +       <property name="rootIsDecorated">
  292. +        <bool>false</bool>
  293. +       </property>
  294. +       <property name="sortingEnabled">
  295. +        <bool>true</bool>
  296. +       </property>
  297. +       <property name="headerHidden">
  298. +        <bool>true</bool>
  299. +       </property>
  300. +       <property name="expandsOnDoubleClick">
  301. +        <bool>false</bool>
  302. +       </property>
  303. +      </widget>
  304.       </item>
  305.      </layout>
  306.     </item>
  307. diff --git a/src/psithememodel.cpp b/src/psithememodel.cpp
  308. index 9720b43a..4f7ec3b2 100644
  309. --- a/src/psithememodel.cpp
  310. +++ b/src/psithememodel.cpp
  311. @@ -114,8 +114,7 @@ void PsiThemeModel::setType(const QString &type)
  312.             themesFuture = QtConcurrent::mapped(provider->themeIds(), loader);
  313.             themeWatcher.setFuture(themesFuture);
  314.         } else {
  315. -
  316. -           foreach (const QString id, provider->themeIds()) {
  317. +           foreach (const QString &id, provider->themeIds()) {
  318.                 loader.asyncLoad(id, [this](const ThemeItemInfo &ti) {
  319.                     if (ti.isValid) {
  320.                         beginInsertRows(QModelIndex(), themesInfo.size(), themesInfo.size());
  321. @@ -125,6 +124,7 @@ void PsiThemeModel::setType(const QString &type)
  322.                         //endResetModel();
  323.                     }
  324.                 });
  325. +
  326.             }
  327.         }
  328.     }
  329. diff --git a/src/psithemeviewdelegate.cpp b/src/psithemeviewdelegate.cpp
  330. deleted file mode 100644
  331. index 81e4e9e5..00000000
  332. --- a/src/psithemeviewdelegate.cpp
  333. +++ /dev/null
  334. @@ -1,86 +0,0 @@
  335. -/*
  336. - * psithemeviewdelegate.cpp - renders theme items
  337. - * Copyright (C) 2010 Rion (Sergey Ilinyh)
  338. - *
  339. - * This program is free software; you can redistribute it and/or
  340. - * modify it under the terms of the GNU General Public License
  341. - * as published by the Free Software Foundation; either version 2
  342. - * of the License, or (at your option) any later version.
  343. - *
  344. - * This program is distributed in the hope that it will be useful,
  345. - * but WITHOUT ANY WARRANTY; without even the implied warranty of
  346. - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  347. - * GNU General Public License for more details.
  348. - *
  349. - * You should have received a copy of the GNU General Public License
  350. - * along with this library; if not, write to the Free Software
  351. - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  352. - *
  353. - */
  354. -
  355. -#include "psithemeviewdelegate.h"
  356. -#include "psithememodel.h"
  357. -#include <QPainter>
  358. -#include <QFontMetrics>
  359. -#include "iconset.h"
  360. -
  361. -// painting
  362. -void PsiThemeViewDelegate::paint(QPainter *painter,
  363. -                  const QStyleOptionViewItem &option,
  364. -                  const QModelIndex &index) const
  365. -{
  366. -   QPixmap screenshot = index.data(PsiThemeModel::ScreenshotRole).value<QPixmap>();
  367. -   QPixmap texture = IconsetFactory::iconPixmap("psi/themeTitleTexture");
  368. -   int texturew = texture.width();
  369. -   int textureh = texture.height();
  370. -
  371. -   QFont f("Serif");
  372. -   f.setBold(true);
  373. -   if (screenshot.isNull()) {
  374. -       f.setPixelSize(20);
  375. -       painter->setFont(f);
  376. -       painter->setPen(QColor(170, 170, 170));
  377. -       QRect nirect(0, option.rect.top() + textureh, option.rect.width(), 20);
  378. -       painter->drawText(nirect, Qt::AlignCenter, tr("No Image"));
  379. -   } else {
  380. -       QRect sp = screenshot.rect();
  381. -       sp.moveTopLeft(option.rect.topLeft());
  382. -       painter->drawPixmap(sp, screenshot);
  383. -   }
  384. -
  385. -   f.setItalic(true);
  386. -   f.setPixelSize(textureh - 8);
  387. -   painter->setFont(f);
  388. -   QFontMetrics fm(f);
  389. -   QString text = index.data(PsiThemeModel::TitleRole).toString();
  390. -   QSize textSize = fm.size(Qt::TextSingleLine, text);
  391. -   int tw = textSize.width() + 10;
  392. -   int y = option.rect.top();
  393. -   int vw = option.rect.width();
  394. -
  395. -   painter->drawPixmap(0, y, vw - tw - texturew, textureh, texture.copy(0, 0, 1, textureh).scaled(vw - tw - texturew, textureh));
  396. -   painter->drawPixmap(vw - tw - texturew, y, texturew, textureh, texture);
  397. -   painter->drawPixmap(vw - tw, y, tw, textureh, texture.copy(texturew - 1, 0, 1, textureh).scaled(tw, textureh));
  398. -
  399. -
  400. -   painter->setPen(option.state & QStyle::State_Selected? Qt::white : QColor(170, 170, 170));
  401. -   QRect txtr(vw - tw, y, tw, textureh-5); // 5 shadow size?
  402. -   painter->drawText(txtr, Qt::AlignCenter, text);
  403. -}
  404. -
  405. -QSize PsiThemeViewDelegate::sizeHint(const QStyleOptionViewItem &option,
  406. -                      const QModelIndex &index) const
  407. -{
  408. -   int textureh = IconsetFactory::iconPixmap("psi/themeTitleTexture").height();
  409. -   QPixmap screenshot = index.data(PsiThemeModel::ScreenshotRole).value<QPixmap>();
  410. -   int h = screenshot.isNull()? textureh + 20 : qMax(textureh, screenshot.height());
  411. -   return QSize(option.rect.width(), h);
  412. -}
  413. -
  414. -// editing
  415. -QWidget *PsiThemeViewDelegate::createEditor(QWidget */*parent*/,
  416. -                             const QStyleOptionViewItem &/*option*/,
  417. -                             const QModelIndex &/*index*/) const
  418. -{
  419. -   return 0;
  420. -}
  421. diff --git a/src/psithemeviewdelegate.h b/src/psithemeviewdelegate.h
  422. deleted file mode 100644
  423. index 8ee8026b..00000000
  424. --- a/src/psithemeviewdelegate.h
  425. +++ /dev/null
  426. @@ -1,49 +0,0 @@
  427. -/*
  428. - * psithemeviewdelegate.h - renders theme items
  429. - * Copyright (C) 2010 Rion (Sergey Ilinyh)
  430. - *
  431. - * This program is free software; you can redistribute it and/or
  432. - * modify it under the terms of the GNU General Public License
  433. - * as published by the Free Software Foundation; either version 2
  434. - * of the License, or (at your option) any later version.
  435. - *
  436. - * This program is distributed in the hope that it will be useful,
  437. - * but WITHOUT ANY WARRANTY; without even the implied warranty of
  438. - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  439. - * GNU General Public License for more details.
  440. - *
  441. - * You should have received a copy of the GNU General Public License
  442. - * along with this library; if not, write to the Free Software
  443. - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  444. - *
  445. - */
  446. -
  447. -#ifndef PSITHEMEVIEWDELEGATE_H
  448. -#define PSITHEMEVIEWDELEGATE_H
  449. -
  450. -#include <QAbstractItemDelegate>
  451. -
  452. -class PsiThemeViewDelegate : public QAbstractItemDelegate
  453. -{
  454. -   Q_OBJECT
  455. -public:
  456. -   PsiThemeViewDelegate ( QObject * parent = 0 )
  457. -       : QAbstractItemDelegate ( parent ) { }
  458. -
  459. -   // painting
  460. -   virtual void paint(QPainter *painter,
  461. -                      const QStyleOptionViewItem &option,
  462. -                      const QModelIndex &index) const;
  463. -
  464. -   virtual QSize sizeHint(const QStyleOptionViewItem &option,
  465. -                          const QModelIndex &index) const;
  466. -
  467. -   // editing
  468. -   virtual QWidget *createEditor(QWidget *parent,
  469. -                                 const QStyleOptionViewItem &option,
  470. -                                 const QModelIndex &index) const;
  471. -
  472. -   static const int TextPadding = 3;
  473. -};
  474. -
  475. -#endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement