syncthingtray/tray/gui/dirbuttonsitemdelegate.cpp

64 lines
2.3 KiB
C++
Raw Permalink Normal View History

2016-08-25 00:45:32 +02:00
#include "./dirbuttonsitemdelegate.h"
#include "./helper.h"
#include <syncthingmodel/syncthingdirectorymodel.h>
#include <syncthingmodel/syncthingicons.h>
#include <qtforkawesome/icon.h>
2021-10-15 22:18:54 +02:00
#include <qtforkawesome/renderer.h>
2017-02-23 15:49:58 +01:00
2016-08-25 00:45:32 +02:00
#include <QApplication>
#include <QBrush>
2017-05-01 03:34:43 +02:00
#include <QPainter>
2016-08-25 00:45:32 +02:00
#include <QPalette>
2017-05-01 03:34:43 +02:00
#include <QPixmap>
#include <QStyle>
#include <QStyleOptionViewItem>
#include <QTextOption>
2016-08-25 00:45:32 +02:00
2017-02-23 15:49:58 +01:00
using namespace Data;
2016-08-25 00:45:32 +02:00
namespace QtGui {
2017-05-01 03:34:43 +02:00
DirButtonsItemDelegate::DirButtonsItemDelegate(QObject *parent)
: QStyledItemDelegate(parent)
{
}
2016-08-25 00:45:32 +02:00
void DirButtonsItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
auto opt = option;
initStyleOption(&opt, index);
opt.viewItemPosition = QStyleOptionViewItem::OnlyOne;
2017-05-01 03:34:43 +02:00
if (index.parent().isValid()) {
QStyledItemDelegate::paint(painter, opt, index);
2016-08-25 00:45:32 +02:00
} else {
// init style options to use drawControl(), except for the text
opt.text.clear();
opt.features = QStyleOptionViewItem::None;
drawBasicItemViewItem(*painter, opt);
2016-08-25 00:45:32 +02:00
// draw text
auto textRect = QRectF(option.rect);
2017-02-23 15:49:58 +01:00
textRect.setWidth(textRect.width() - 58);
2016-08-25 00:45:32 +02:00
QTextOption textOption;
textOption.setAlignment(opt.displayAlignment);
setupPainterToDrawViewItemText(painter, opt);
2016-08-25 00:45:32 +02:00
painter->drawText(textRect, displayText(index.data(Qt::DisplayRole), option.locale), textOption);
// draw buttons
const int buttonY = option.rect.y() + centerObj(option.rect.height(), 16);
const bool dirPaused = index.data(SyncthingDirectoryModel::DirectoryPaused).toBool();
const auto iconColor = QGuiApplication::palette().color(QPalette::Text);
auto &forkAwesomeRenderer = QtForkAwesome::Renderer::global();
if (!dirPaused) {
forkAwesomeRenderer.render(QtForkAwesome::Icon::Refresh, painter, QRect(option.rect.right() - 52, buttonY, 16, 16), iconColor);
}
2021-10-15 22:18:54 +02:00
forkAwesomeRenderer.render(
dirPaused ? QtForkAwesome::Icon::Play : QtForkAwesome::Icon::Pause, painter, QRect(option.rect.right() - 34, buttonY, 16, 16), iconColor);
forkAwesomeRenderer.render(QtForkAwesome::Icon::Folder, painter, QRect(option.rect.right() - 16, buttonY, 16, 16), iconColor);
2016-08-25 00:45:32 +02:00
}
}
} // namespace QtGui