Changeset 14454


Ignore:
Timestamp:
Jan 17, 2015, 4:59:42 PM (6 years ago)
Author:
mikedld
Message:

Improve overall look of torrents in main window

Refactor and use the same code to calculate element positions in
sizeHint () and paint () to prevent discrepancies.
While we are at it, properly support RTL layout.

Location:
trunk/qt
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/qt/filterbar.cc

    r14445 r14454  
    5454    return fadedColor;
    5555  }
    56 
    57   void
    58   narrowRect (QRect& rect, int dx1, int dx2, Qt::LayoutDirection direction)
    59   {
    60     if (direction == Qt::LeftToRight)
    61       rect.adjust (dx1, 0, -dx2, 0);
    62     else
    63       rect.adjust (dx2, 0, -dx1, 0);
    64   }
    6556}
    6657
     
    118109                                            Qt::AlignLeft|Qt::AlignVCenter,
    119110                                            decorationRect.size (), boundingBox);
    120       narrowRect (boundingBox, decorationRect.width () + hmargin, 0, option.direction);
     111      Utils::narrowRect (boundingBox, decorationRect.width () + hmargin, 0, option.direction);
    121112
    122113      QRect countRect  = rect (option, index, TorrentCountStringRole);
     
    124115                                       Qt::AlignRight|Qt::AlignVCenter,
    125116                                       countRect.size (), boundingBox);
    126       narrowRect (boundingBox, 0, countRect.width () + hmargin, option.direction);
     117      Utils::narrowRect (boundingBox, 0, countRect.width () + hmargin, option.direction);
    127118      const QRect displayRect = boundingBox;
    128119
     
    264255                                                     opt.iconSize, rect);
    265256          painter.drawPixmap (iconRect.topLeft (), pixmap);
    266           narrowRect (rect, iconRect.width () + hmargin, 0, opt.direction);
     257          Utils::narrowRect (rect, iconRect.width () + hmargin, 0, opt.direction);
    267258        }
    268259
     
    276267                                                     QSize (opt.fontMetrics.width (text), rect.height ()), rect);
    277268          painter.drawText (textRect, Qt::AlignRight | Qt::AlignVCenter, text);
    278           narrowRect (rect, 0, textRect.width () + hmargin, opt.direction);
     269          Utils::narrowRect (rect, 0, textRect.width () + hmargin, opt.direction);
    279270          painter.setPen (pen);
    280271        }
  • trunk/qt/torrent-delegate-min.cc

    r14453 r14454  
    2727#include "torrent-delegate-min.h"
    2828#include "torrent-model.h"
     29#include "utils.h"
    2930
    3031enum
     
    4445***/
    4546
     47namespace
     48{
     49  class ItemLayout
     50  {
     51    private:
     52      QString myNameText;
     53      QString myStatusText;
     54
     55    public:
     56      QFont nameFont;
     57      QFont statusFont;
     58
     59      QRect iconRect;
     60      QRect emblemRect;
     61      QRect nameRect;
     62      QRect statusRect;
     63      QRect barRect;
     64
     65    public:
     66      ItemLayout(const QString& nameText, const QString& statusText, const QIcon& emblemIcon,
     67                 const QFont& baseFont, Qt::LayoutDirection direction, const QPoint& topLeft, int width);
     68
     69      QSize size () const
     70      {
     71        return (iconRect | nameRect | statusRect | barRect).size ();
     72      }
     73
     74      QString nameText () const { return elidedText (nameFont, myNameText, nameRect.width ()); }
     75      QString statusText () const { return myStatusText; }
     76
     77    private:
     78      QString elidedText (const QFont& font, const QString& text, int width) const
     79      {
     80        return QFontMetrics (font).elidedText (text, Qt::ElideRight, width);
     81      }
     82  };
     83
     84  ItemLayout::ItemLayout(const QString& nameText, const QString& statusText, const QIcon& emblemIcon,
     85                         const QFont& baseFont, Qt::LayoutDirection direction, const QPoint& topLeft, int width):
     86    myNameText (nameText),
     87    myStatusText (statusText),
     88    nameFont (baseFont),
     89    statusFont (baseFont)
     90  {
     91    const QStyle * style (qApp->style ());
     92    const int iconSize (style->pixelMetric (QStyle::PM_SmallIconSize));
     93
     94    const QFontMetrics nameFM (nameFont);
     95    const QSize nameSize (nameFM.size (0, myNameText));
     96
     97    statusFont.setPointSize (static_cast<int> (statusFont.pointSize () * 0.85));
     98    const QFontMetrics statusFM (statusFont);
     99    const QSize statusSize (statusFM.size (0, myStatusText));
     100
     101    QRect baseRect (topLeft, QSize (width, qMax (iconSize, qMax (nameSize.height (), qMax (statusSize.height (), static_cast<int>(BAR_HEIGHT))))));
     102
     103    iconRect = style->alignedRect (direction, Qt::AlignLeft | Qt::AlignVCenter, QSize (iconSize, iconSize), baseRect);
     104    emblemRect = style->alignedRect (direction, Qt::AlignRight | Qt::AlignBottom,
     105                                     emblemIcon.actualSize (iconRect.size () / 2, QIcon::Normal, QIcon::On),
     106                                     iconRect);
     107    barRect = style->alignedRect (direction, Qt::AlignRight | Qt::AlignVCenter, QSize (BAR_WIDTH, BAR_HEIGHT), baseRect);
     108    Utils::narrowRect (baseRect, iconRect.width () + GUI_PAD, barRect.width () + GUI_PAD, direction);
     109    statusRect = style->alignedRect (direction, Qt::AlignRight | Qt::AlignVCenter, QSize (statusSize.width (), baseRect.height ()), baseRect);
     110    Utils::narrowRect (baseRect, 0, statusRect.width () + GUI_PAD, direction);
     111    nameRect = baseRect;
     112  }
     113}
     114
    46115QSize
    47116TorrentDelegateMin::sizeHint (const QStyleOptionViewItem & option,
    48117                              const Torrent              & tor) const
    49118{
    50   const QStyle* style (qApp->style());
    51   static const int iconSize (style->pixelMetric (QStyle::PM_SmallIconSize));
    52 
    53   QFont nameFont (option.font);
    54   const QFontMetrics nameFM (nameFont);
    55119  const bool isMagnet (!tor.hasMetadata());
    56   const QString nameStr = (isMagnet ? progressString (tor) : tor.name());
    57   const int nameWidth = nameFM.width (nameStr);
    58 
    59   QFont statusFont (option.font);
    60   statusFont.setPointSize (static_cast<int> (option.font.pointSize() * 0.85));
    61   const QFontMetrics statusFM (statusFont);
    62   const QString statusStr (shortStatusString (tor));
    63   const int statusWidth = statusFM.width (statusStr);
    64 
    65   const QSize m (margin (*style));
    66 
    67   return QSize (m.width()*2 + iconSize + GUI_PAD + nameWidth
    68                                        + GUI_PAD + statusWidth
    69                                        + GUI_PAD + BAR_WIDTH,
    70                 m.height()*2 + std::max (nameFM.height(), (int)BAR_HEIGHT));
     120  const QSize m (margin (*qApp->style()));
     121  const ItemLayout layout (isMagnet ? progressString (tor) : tor.name(), shortStatusString (tor), QIcon (),
     122                           option.font, option.direction, QPoint (0, 0), option.rect.width () - m.width () * 2);
     123  return layout.size () + m * 2;
    71124}
    72125
     
    76129                                 const Torrent              & tor) const
    77130{
     131  const QStyle * style (qApp->style());
     132
    78133  const bool isPaused (tor.isPaused());
    79   const QStyle * style (qApp->style());
    80   static const int iconSize (style->pixelMetric (QStyle::PM_SmallIconSize));
    81 
    82   QFont nameFont (option.font);
    83   const QFontMetrics nameFM (nameFont);
    84134  const bool isMagnet (!tor.hasMetadata());
    85   const QString nameStr = (isMagnet ? progressString (tor) : tor.name());
    86 
    87   QFont statusFont (option.font);
    88   statusFont.setPointSize (static_cast<int> (option.font.pointSize() * 0.85));
    89   const QFontMetrics statusFM (statusFont);
    90   const QString statusStr (shortStatusString (tor));
    91   const QSize statusSize (statusFM.size (0, statusStr));
    92135
    93136  const bool isItemSelected ((option.state & QStyle::State_Selected) != 0);
     
    142185  // layout
    143186  const QSize m (margin (*style));
    144   QRect fillArea (option.rect);
    145   fillArea.adjust (m.width(), m.height(), -m.width(), -m.height());
    146   const QRect iconArea (fillArea.x(),
    147                         fillArea.y() +  (fillArea.height() - iconSize) / 2,
    148                         iconSize,
    149                         iconSize);
    150   const QRect emblemRect (style->alignedRect (option.direction, Qt::AlignRight | Qt::AlignBottom,
    151                                               emblemIcon.actualSize (QSize (iconSize / 2, iconSize / 2), emblemIm, qs),
    152                                               iconArea));
    153   const QRect barArea (fillArea.x() + fillArea.width() - BAR_WIDTH,
    154                        fillArea.y() +  (fillArea.height() - BAR_HEIGHT) / 2,
    155                        BAR_WIDTH,
    156                        BAR_HEIGHT);
    157   const QRect statusArea (barArea.x() - GUI_PAD - statusSize.width(),
    158                           fillArea.y() +  (fillArea.height() - statusSize.height()) / 2,
    159                           fillArea.width(),
    160                           fillArea.height());
    161   const QRect nameArea (iconArea.x() + iconArea.width() + GUI_PAD,
    162                         fillArea.y(),
    163                         statusArea.x() -  (iconArea.x() + iconArea.width() + GUI_PAD * 2),
    164                         fillArea.height());
     187  const QRect contentRect (option.rect.adjusted (m.width(), m.height(), -m.width(), -m.height()));
     188  const ItemLayout layout (isMagnet ? progressString (tor) : tor.name(), shortStatusString (tor), emblemIcon,
     189                           option.font, option.direction, contentRect.topLeft (), contentRect.width ());
    165190
    166191  // render
     
    169194  else
    170195    painter->setPen (option.palette.color (cg, cr));
    171   tor.getMimeTypeIcon().paint (painter, iconArea, Qt::AlignCenter, im, qs);
     196  tor.getMimeTypeIcon().paint (painter, layout.iconRect, Qt::AlignCenter, im, qs);
    172197  if (!emblemIcon.isNull ())
    173     emblemIcon.paint (painter, emblemRect, Qt::AlignCenter, emblemIm, qs);
    174   painter->setFont (nameFont);
    175   painter->drawText (nameArea, 0, nameFM.elidedText (nameStr, Qt::ElideRight, nameArea.width()));
    176   painter->setFont (statusFont);
    177   painter->drawText (statusArea, 0, statusStr);
    178   myProgressBarStyle->rect = barArea;
     198    emblemIcon.paint (painter, layout.emblemRect, Qt::AlignCenter, emblemIm, qs);
     199  painter->setFont (layout.nameFont);
     200  painter->drawText (layout.nameRect, Qt::AlignLeft | Qt::AlignVCenter, layout.nameText ());
     201  painter->setFont (layout.statusFont);
     202  painter->drawText (layout.statusRect, Qt::AlignLeft | Qt::AlignVCenter, layout.statusText ());
     203  myProgressBarStyle->rect = layout.barRect;
    179204  if (tor.isDownloading())
    180205    {
  • trunk/qt/torrent-delegate.cc

    r14453 r14454  
    2525#include "torrent-delegate.h"
    2626#include "torrent-model.h"
     27#include "utils.h"
    2728
    2829enum
     
    3839QColor TorrentDelegate::blueBack;
    3940QColor TorrentDelegate::silverBack;
     41
     42namespace
     43{
     44  class ItemLayout
     45  {
     46    private:
     47      QString myNameText;
     48      QString myStatusText;
     49      QString myProgressText;
     50
     51    public:
     52      QFont nameFont;
     53      QFont statusFont;
     54      QFont progressFont;
     55
     56      QRect iconRect;
     57      QRect emblemRect;
     58      QRect nameRect;
     59      QRect statusRect;
     60      QRect barRect;
     61      QRect progressRect;
     62
     63    public:
     64      ItemLayout(const QString& nameText, const QString& statusText, const QString& progressText,
     65                 const QIcon& emblemIcon, const QFont& baseFont, Qt::LayoutDirection direction,
     66                 const QPoint& topLeft, int width);
     67
     68      QSize size () const
     69      {
     70        return (iconRect | nameRect | statusRect | barRect | progressRect).size ();
     71      }
     72
     73      QString nameText () const { return elidedText (nameFont, myNameText, nameRect.width ()); }
     74      QString statusText () const { return elidedText (statusFont, myStatusText, statusRect.width ()); }
     75      QString progressText () const  { return elidedText (progressFont, myProgressText, progressRect.width ()); }
     76
     77    private:
     78      QString elidedText (const QFont& font, const QString& text, int width) const
     79      {
     80        return QFontMetrics (font).elidedText (text, Qt::ElideRight, width);
     81      }
     82  };
     83
     84  ItemLayout::ItemLayout(const QString& nameText, const QString& statusText, const QString& progressText,
     85                         const QIcon& emblemIcon, const QFont& baseFont, Qt::LayoutDirection direction,
     86                         const QPoint& topLeft, int width):
     87    myNameText (nameText),
     88    myStatusText (statusText),
     89    myProgressText (progressText),
     90    nameFont (baseFont),
     91    statusFont (baseFont),
     92    progressFont (baseFont)
     93  {
     94    const QStyle * style (qApp->style ());
     95    const int iconSize (style->pixelMetric (QStyle::PM_LargeIconSize));
     96
     97    nameFont.setWeight (QFont::Bold);
     98    const QFontMetrics nameFM (nameFont);
     99    const QSize nameSize (nameFM.size (0, myNameText));
     100
     101    statusFont.setPointSize (static_cast<int> (statusFont.pointSize () * 0.9));
     102    const QFontMetrics statusFM (statusFont);
     103    const QSize statusSize (statusFM.size (0, myStatusText));
     104
     105    progressFont.setPointSize (static_cast<int> (progressFont.pointSize () * 0.9));
     106    const QFontMetrics progressFM (progressFont);
     107    const QSize progressSize (progressFM.size (0, myProgressText));
     108
     109    QRect baseRect (topLeft, QSize (width, 0));
     110    Utils::narrowRect (baseRect, iconSize + GUI_PAD, 0, direction);
     111
     112    nameRect = baseRect.adjusted(0, 0, 0, nameSize.height ());
     113    statusRect = nameRect.adjusted(0, nameRect.height () + 1, 0, statusSize.height () + 1);
     114    barRect = statusRect.adjusted(0, statusRect.height () + 1, 0, BAR_HEIGHT + 1);
     115    progressRect = barRect.adjusted (0, barRect.height () + 1, 0, progressSize.height () + 1);
     116    iconRect = style->alignedRect (direction, Qt::AlignLeft | Qt::AlignVCenter,
     117                                   QSize (iconSize, iconSize),
     118                                   QRect (topLeft, QSize (width, progressRect.bottom () - nameRect.top ())));
     119    emblemRect = style->alignedRect (direction, Qt::AlignRight | Qt::AlignBottom,
     120                                     emblemIcon.actualSize (iconRect.size () / 2, QIcon::Normal, QIcon::On),
     121                                     iconRect);
     122  }
     123}
    40124
    41125TorrentDelegate::TorrentDelegate (QObject * parent):
     
    169253    }
    170254
    171     return str;
     255    return str.trimmed ();
    172256}
    173257
     
    188272    str = Formatter::uploadSpeedToString(tor.uploadSpeed());
    189273
    190   return str;
     274  return str.trimmed ();
    191275}
    192276
     
    216300    }
    217301
    218   return str;
     302  return str.trimmed ();
    219303}
    220304
     
    272356    }
    273357
    274   return str;
     358  return str.trimmed ();
    275359}
    276360
     
    279363***/
    280364
    281 namespace
    282 {
    283   int MAX3 (int a, int b, int c)
    284     {
    285       const int ab (a > b ? a : b);
    286       return ab > c ? ab : c;
    287     }
    288 }
    289 
    290365QSize
    291366TorrentDelegate::sizeHint (const QStyleOptionViewItem& option, const Torrent& tor) const
    292367{
    293   const QStyle* style (qApp->style ());
    294   static const int iconSize (style->pixelMetric (QStyle::PM_MessageBoxIconSize));
    295 
    296   QFont nameFont (option.font);
    297   nameFont.setWeight (QFont::Bold);
    298   const QFontMetrics nameFM (nameFont);
    299   const QString nameStr (tor.name ());
    300   const int nameWidth = nameFM.width (nameStr);
    301   QFont statusFont (option.font);
    302   statusFont.setPointSize (static_cast<int> (option.font.pointSize () * 0.9));
    303   const QFontMetrics statusFM (statusFont);
    304   const QString statusStr (statusString (tor));
    305   const int statusWidth = statusFM.width (statusStr);
    306   QFont progressFont (statusFont);
    307   const QFontMetrics progressFM (progressFont);
    308   const QString progressStr (progressString (tor));
    309   const int progressWidth = progressFM.width (progressStr);
    310   const QSize m (margin (*style));
    311   return QSize (m.width()*2 + iconSize + GUI_PAD + MAX3 (nameWidth, statusWidth, progressWidth),
    312                 //m.height()*3 + nameFM.lineSpacing() + statusFM.lineSpacing()*2 + progressFM.lineSpacing());
    313                 m.height()*3 + nameFM.lineSpacing() + statusFM.lineSpacing() + BAR_HEIGHT + progressFM.lineSpacing());
     368  const QSize m (margin (*qApp->style ()));
     369  const ItemLayout layout (tor.name (), progressString (tor), statusString (tor), QIcon (),
     370                           option.font, option.direction, QPoint (0, 0), option.rect.width () - m.width () * 2);
     371  return layout.size () + m * 2;
    314372}
    315373
     
    359417{
    360418  const QStyle * style (qApp->style ());
    361   static const int iconSize (style->pixelMetric (QStyle::PM_LargeIconSize));
    362   QFont nameFont (option.font);
    363   nameFont.setWeight (QFont::Bold);
    364   const QFontMetrics nameFM (nameFont);
    365   const QString nameStr (tor.name ());
    366   const QSize nameSize (nameFM.size (0, nameStr));
    367   QFont statusFont (option.font);
    368   statusFont.setPointSize (static_cast<int> (option.font.pointSize () * 0.9));
    369   const QFontMetrics statusFM (statusFont);
    370   const QString statusStr (progressString (tor));
    371   QFont progressFont (statusFont);
    372   const QFontMetrics progressFM (progressFont);
    373   const QString progressStr (statusString (tor));
     419
    374420  const bool isPaused (tor.isPaused ());
    375421
     
    425471  // layout
    426472  const QSize m (margin (*style));
    427   QRect fillArea (option.rect);
    428   fillArea.adjust (m.width(), m.height(), -m.width(), -m.height());
    429   QRect iconArea (fillArea.x (), fillArea.y () +  (fillArea.height () - iconSize) / 2, iconSize, iconSize);
    430   QRect emblemRect (style->alignedRect (option.direction, Qt::AlignRight | Qt::AlignBottom,
    431                                         emblemIcon.actualSize (QSize (iconSize / 2, iconSize / 2), emblemIm, qs), iconArea));
    432   QRect nameArea (iconArea.x () + iconArea.width () + GUI_PAD, fillArea.y (),
    433                   fillArea.width () - GUI_PAD - iconArea.width (), nameSize.height ());
    434   QRect statusArea (nameArea);
    435   statusArea.moveTop (nameArea.y () + nameFM.lineSpacing ());
    436   statusArea.setHeight (nameSize.height ());
    437   QRect barArea (statusArea);
    438   barArea.setHeight (BAR_HEIGHT);
    439   barArea.moveTop (statusArea.y () + statusFM.lineSpacing ());
    440   QRect progArea (statusArea);
    441   progArea.moveTop (barArea.y () + barArea.height ());
     473  const QRect contentRect (option.rect.adjusted (m.width(), m.height(), -m.width(), -m.height()));
     474  const ItemLayout layout (tor.name (), progressString (tor), statusString (tor), emblemIcon,
     475                           option.font, option.direction, contentRect.topLeft (), contentRect.width ());
    442476
    443477  // render
     
    446480  else
    447481    painter->setPen (option.palette.color (cg, cr));
    448   tor.getMimeTypeIcon().paint (painter, iconArea, Qt::AlignCenter, im, qs);
     482  tor.getMimeTypeIcon().paint (painter, layout.iconRect, Qt::AlignCenter, im, qs);
    449483  if (!emblemIcon.isNull ())
    450     emblemIcon.paint (painter, emblemRect, Qt::AlignCenter, emblemIm, qs);
    451   painter->setFont (nameFont);
    452   painter->drawText (nameArea, 0, nameFM.elidedText (nameStr, Qt::ElideRight, nameArea.width ()));
    453   painter->setFont (statusFont);
    454   painter->drawText (statusArea, 0, statusFM.elidedText (statusStr, Qt::ElideRight, statusArea.width ()));
    455   painter->setFont (progressFont);
    456   painter->drawText (progArea, 0, progressFM.elidedText (progressStr, Qt::ElideRight, progArea.width ()));
    457   myProgressBarStyle->rect = barArea;
     484    emblemIcon.paint (painter, layout.emblemRect, Qt::AlignCenter, emblemIm, qs);
     485  painter->setFont (layout.nameFont);
     486  painter->drawText (layout.nameRect, Qt::AlignLeft | Qt::AlignVCenter, layout.nameText ());
     487  painter->setFont (layout.statusFont);
     488  painter->drawText (layout.statusRect, Qt::AlignLeft | Qt::AlignVCenter, layout.statusText ());
     489  painter->setFont (layout.progressFont);
     490  painter->drawText (layout.progressRect, Qt::AlignLeft | Qt::AlignVCenter, layout.progressText ());
     491  myProgressBarStyle->rect = layout.barRect;
    458492  if (tor.isDownloading())
    459493    {
  • trunk/qt/utils.h

    r14241 r14454  
    1111#define QTR_UTILS
    1212
     13#include <QIcon>
     14#include <QObject>
     15#include <QRect>
    1316#include <QString>
    14 #include <QObject>
    15 #include <QIcon>
    1617
    1718#include <cctype> // isxdigit()
     
    3435
    3536    static QString removeTrailingDirSeparator (const QString& path);
     37
     38    static void narrowRect (QRect& rect, int dx1, int dx2, Qt::LayoutDirection direction)
     39    {
     40      if (direction == Qt::RightToLeft)
     41        qSwap (dx1, dx2);
     42      rect.adjust (dx1, 0, -dx2, 0);
     43    }
    3644
    3745    // meh
Note: See TracChangeset for help on using the changeset viewer.