Advertisement
Guest User

new theme interface

a guest
Apr 30th, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.27 KB | None | 0 0
  1. diff --git a/src/options/opt_theme.cpp b/src/options/opt_theme.cpp
  2. index 3914f15e..9e0a9f2f 100644
  3. --- a/src/options/opt_theme.cpp
  4. +++ b/src/options/opt_theme.cpp
  5. @@ -6,12 +6,11 @@
  6.  #include "psithememanager.h"
  7.  #include "psiiconset.h"
  8.  
  9. -#include <QToolButton>
  10. -#include <QDialog>
  11.  #include <QSortFilterProxyModel>
  12.  #include <QTimer>
  13. +#include <QBuffer>
  14.  
  15. -#define SCREEN_PREFIX "scb_"
  16. +#define LABEL_PREFIX "tl_"
  17.  
  18.  class OptAppearanceThemeUI : public QWidget, public Ui::OptAppearanceTheme
  19.  {
  20. @@ -91,6 +90,7 @@ void OptionsTabAppearanceTheme::startLoading()
  21.  
  22.  void OptionsTabAppearanceTheme::themeSelected(const QModelIndex &current, const QModelIndex &previous)
  23.  {
  24. +   Q_UNUSED(current)
  25.     if (!previous.isValid()) {
  26.         return; // Psi won't start if it's impossible to load any theme. So we always have previous.
  27.     }
  28. @@ -101,7 +101,6 @@ void OptionsTabAppearanceTheme::modelRowsInserted(const QModelIndex &parent, int
  29.  {
  30.     if (!parent.isValid() || !w) {
  31.         OptAppearanceThemeUI *d = (OptAppearanceThemeUI *)w;
  32. -       const QSize buttonSize = QSize(21,21);
  33.         for (int i = first; i <= last; i++) {
  34.             const QModelIndex index = themesModel->index(i, 0);
  35.             const QString id = themesModel->data(index, PsiThemeModel::IdRole).toString();
  36. @@ -110,79 +109,44 @@ void OptionsTabAppearanceTheme::modelRowsInserted(const QModelIndex &parent, int
  37.             }
  38.             const QString themeName = themesModel->data(index, PsiThemeModel::TitleRole).toString();
  39.             bool isPsi = id.startsWith("psi");
  40. -           const QPixmap client = isPsi ? IconsetFactory::iconPixmap("clients/psi")
  41. -                            : IconsetFactory::iconPixmap("clients/adium");
  42. +           QPixmap client = isPsi ? IconsetFactory::iconPixmap("clients/psi")
  43. +                          : IconsetFactory::iconPixmap("clients/adium");
  44.             const QString clientName = isPsi ? tr("Psi Theme") : tr("Adium Theme");
  45. -           QToolButton *screenshotButton = new QToolButton(d->themeView);
  46. -           screenshotButton->setIcon(QIcon(IconsetFactory::iconPixmap("psi/eye")));
  47. -           screenshotButton->resize(buttonSize);
  48. -           screenshotButton->setObjectName(SCREEN_PREFIX + id);
  49. -           screenshotButton->setToolTip(tr("Show theme screenshot"));
  50. -           connect(screenshotButton, SIGNAL(clicked()), this, SLOT(showThemeScreenshot()));
  51.  
  52.             QLabel *typeLabel = new QLabel(d->themeView);
  53. -           typeLabel->setPixmap(client);
  54. -           typeLabel->setToolTip(clientName);
  55. -
  56. -           QLabel *nameLabel = new QLabel(themeName, d->themeView);
  57. -
  58. -           QWidget *itemWidget = new QWidget(d->themeView);
  59. -
  60. -           QBoxLayout *box = new QBoxLayout(QBoxLayout::LeftToRight, d->themeView);
  61. -           box->addWidget(typeLabel);
  62. -           box->addWidget(nameLabel);
  63. -           box->addStretch();
  64. -           box->addWidget(screenshotButton);
  65. -           itemWidget->setLayout(box);
  66. -           //itemWidget->setAutoFillBackground(true); // from recommendation of indexWidget but does not work as expected
  67. -
  68. -           d->themeView->setIndexWidget(index, itemWidget);
  69. +           typeLabel->setObjectName(LABEL_PREFIX + id);
  70. +           QString itemContents(themeName);
  71. +           if (!client.isNull()) {
  72. +               itemContents =QString("<table width='100%' border='0'><tr>"
  73. +                             "<td align='center' width='22'><img src='data:image/png;base64, %0'>"
  74. +                             "</td><td align='left'>%1</td>"
  75. +                             "</tr></table>").arg(getPixmapBytes(client)).arg(themeName);
  76. +           }
  77. +           typeLabel->setText(itemContents);
  78. +           QString html(clientName);
  79. +           QPixmap scr = unsortedModel->data(index, PsiThemeModel::ScreenshotRole).value<QPixmap>();
  80. +           if (!scr.isNull()) {
  81. +               const QString desc = tr("Screenshot of %1 theme").arg(themeName);
  82. +               html = QString("<table border='0'><tr><td align='center'>"
  83. +                          "<b>%0</b>"
  84. +                          "</td></tr><tr><td><img src='data:image/png;base64, %1'></td>"
  85. +                          "</tr></table>").arg(desc).arg(getPixmapBytes(scr));
  86. +           }
  87. +           typeLabel->setToolTip(html);
  88. +           d->themeView->setIndexWidget(index, typeLabel);
  89.         }
  90.         themesModel->sort(0);
  91.     }
  92.  }
  93.  
  94. -void OptionsTabAppearanceTheme::showThemeScreenshot()
  95. +QString OptionsTabAppearanceTheme::getPixmapBytes(const QPixmap &image) const
  96.  {
  97. -   if ( !w || !sender()->inherits("QToolButton") )
  98. -       return;
  99. -   OptAppearanceThemeUI *d = (OptAppearanceThemeUI *)w;
  100. -   QToolButton *btn = static_cast<QToolButton*>(sender());
  101. -   if ( btn ) {
  102. -       if ( screenshotDialog )
  103. -           delete(screenshotDialog);
  104. -
  105. -       const QSize minSize(300, 100);
  106. -       screenshotDialog = new QDialog(d);
  107. -       screenshotDialog->setMinimumSize(minSize);
  108. -
  109. -       const int row = unsortedModel->themeRow(getThemeId(btn->objectName()));
  110. -       const QModelIndex index = unsortedModel->index(row, 0);
  111. -       const QString name_ = unsortedModel->data(index, PsiThemeModel::TitleRole).toString();
  112. -       const QPixmap scr = unsortedModel->data(index, PsiThemeModel::ScreenshotRole).value<QPixmap>();
  113. -
  114. -       screenshotDialog->setWindowTitle(tr("%1 Screenshot").arg(name_));
  115. -       screenshotDialog->setWindowIcon(QIcon(IconsetFactory::iconPixmap("psi/logo_128")));
  116. -
  117. -       QBoxLayout *box = new QBoxLayout(QBoxLayout::LeftToRight, screenshotDialog);
  118. -       QLabel *image = new QLabel(screenshotDialog);
  119. -       if (!scr.isNull()) {
  120. -           image->setPixmap(scr);
  121. -       }
  122. -       else {
  123. -           image->setText(tr("No Image"));
  124. -       }
  125. -       box->addWidget(image);
  126. +   QByteArray imageData;
  127. +   QBuffer imageBuffer(&imageData);
  128. +   imageBuffer.open(QIODevice::WriteOnly);
  129. +   image.save(&imageBuffer, "PNG");
  130. +   return QString(imageData.toBase64());
  131.  
  132. -       screenshotDialog->setAttribute(Qt::WA_DeleteOnClose);
  133. -       screenshotDialog->show();
  134. -   }
  135. -}
  136. -
  137. -QString OptionsTabAppearanceTheme::getThemeId(const QString &objName) const
  138. -{
  139. -   const int index = objName.indexOf("_", 0);
  140. -   return (index >0 ? objName.right(objName.length() - index - 1) : QString());
  141.  }
  142.  
  143.  void OptionsTabAppearanceTheme::applyOptions()
  144. diff --git a/src/options/opt_theme.h b/src/options/opt_theme.h
  145. index 44ad6a81..b39d81c5 100644
  146. --- a/src/options/opt_theme.h
  147. +++ b/src/options/opt_theme.h
  148. @@ -2,14 +2,13 @@
  149.  #define OPT_THEME_H
  150.  
  151.  #include "optionstab.h"
  152. -#include <QPointer>
  153. +#include <QPixmap>
  154.  
  155.  class QModelIndex;
  156.  class QSortFilterProxyModel;
  157.  class QWidget;
  158.  class PsiThemeModel;
  159.  class PsiThemeProvider;
  160. -class QDialog;
  161.  
  162.  class OptionsTabAppearanceThemes : public MetaOptionsTab
  163.  {
  164. @@ -32,20 +31,18 @@ public:
  165.  
  166.  protected slots:
  167.     void modelRowsInserted(const QModelIndex &parent, int first, int last);
  168. -   void showThemeScreenshot();
  169.  
  170.  private slots:
  171.     void themeSelected(const QModelIndex &current, const QModelIndex &previous);
  172.     void startLoading();
  173.  private:
  174. -   QString getThemeId(const QString &objName) const;
  175. +   QString getPixmapBytes(const QPixmap &image) const;
  176.  
  177.  private:
  178.     QWidget *w = nullptr;
  179.     PsiThemeModel *unsortedModel = nullptr;
  180.     QSortFilterProxyModel *themesModel = nullptr;
  181.     PsiThemeProvider *provider = nullptr;
  182. -   QPointer<QDialog> screenshotDialog;
  183.  };
  184.  
  185.  #endif
  186. diff --git a/src/psithememodel.cpp b/src/psithememodel.cpp
  187. index 48cc46d0..f1c95e11 100644
  188. --- a/src/psithememodel.cpp
  189. +++ b/src/psithememodel.cpp
  190. @@ -25,6 +25,7 @@
  191.  
  192.  #include "psithememanager.h"
  193.  
  194. +static const QSize minItemSize(100, 32);
  195.  
  196.  class PsiThemeModel;
  197.  struct PsiThemeModel::Loader
  198. @@ -152,6 +153,8 @@ QVariant PsiThemeModel::data ( const QModelIndex & index, int role ) const
  199.         }
  200.         case IsCurrent:
  201.             return themesInfo[index.row()].isCurrent;
  202. +       case Qt::SizeHintRole:
  203. +           return minItemSize;
  204.     }
  205.     return QVariant();
  206.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement