File: viewers.cpp

package info (click to toggle)
psi-plus 1.4.1456-2.2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 26,188 kB
  • sloc: cpp: 211,254; ansic: 19,786; javascript: 13,687; xml: 4,056; sh: 1,610; makefile: 437; objc: 407; python: 277; ruby: 171
file content (133 lines) | stat: -rw-r--r-- 4,635 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
 * viewers.cpp - plugin
 * Copyright (C) 2009-2010  Evgeny Khryukin
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 */

#include "viewers.h"

#include <QHeaderView>
#include <QMenu>
#include <QPainter>
#include <QStyleOptionViewItem>

#include "iconfactoryaccessinghost.h"

//------------------------------------------
//-----------ClearingViewer-----------------
//------------------------------------------
void ClearingViewer::init(IconFactoryAccessingHost *iconHost)
{
    iconHost_ = iconHost;
    resizeColumnsToContents();
    horizontalHeader()->setSectionResizeMode(0, QHeaderView::ResizeToContents);
    horizontalHeader()->setStretchLastSection(true);
    horizontalHeader()->setSortIndicator(-1, Qt::AscendingOrder);
    verticalHeader()->setDefaultAlignment(Qt::AlignHCenter);

    // TODO: update after stopping support of Ubuntu Xenial:
    connect(horizontalHeader(), SIGNAL(sectionClicked(int)), this, SLOT(sortByColumn(int)));
    connect(this, &ClearingViewer::clicked, this, &ClearingViewer::itemClicked);
}

void ClearingViewer::keyPressEvent(QKeyEvent *e)
{
    if (e->key() == Qt::Key_Space) {
        for (const QModelIndex &check : selectionModel()->selectedRows(0)) {
            model()->setData(check, 3); // invert
        }
        e->accept();
    } else {
        QTableView::keyPressEvent(e);
        e->ignore();
    }
}

void ClearingViewer::contextMenuEvent(QContextMenuEvent *e)
{
    QMenu *          popup = new QMenu(this);
    QList<QAction *> actions;
    actions << new QAction(iconHost_->getIcon("psi/cm_check"), tr("Check"), popup)
            << new QAction(iconHost_->getIcon("psi/cm_uncheck"), tr("Uncheck"), popup)
            << new QAction(iconHost_->getIcon("psi/cm_invertcheck"), tr("Invert"), popup);
    popup->addActions(actions);
    QAction *result = popup->exec(e->globalPos());
    int      iresult;
    if (result) {
        iresult = actions.indexOf(result);
        for (const QModelIndex &check : selectionModel()->selectedRows(0)) {
            switch (iresult) {
            case 0: // check
                model()->setData(check, QVariant(2));
                break;
            case 1: // uncheck
                model()->setData(check, QVariant(0));
                break;
            case 2: // invert
                model()->setData(check, QVariant(3));
                break;
            }
        }
    }
    delete popup;
}

void ClearingViewer::itemClicked(const QModelIndex &index)
{
    if (index.column() == 0) {
        model()->setData(currentIndex(), 3); // invert
    }
}

//------------------------------------------
//-----------AvatarDelegate-----------------
//------------------------------------------
QSize AvatarDelegate::sizeHint(const QStyleOptionViewItem & /*option*/, const QModelIndex &index) const
{
    if (!index.isValid())
        return QSize(0, 0);

    return QSize(300, 120);
}

void AvatarDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QPalette palette = option.palette;
    QRect    r       = option.rect;
    QColor   c
        = (option.state & QStyle::State_Selected) ? palette.color(QPalette::Highlight) : palette.color(QPalette::Base);
    painter->fillRect(r, c);

    QPixmap pix = index.data(Qt::DisplayRole).value<QPixmap>();
    painter->save();
    painter->setClipRect(r);
    if (!pix.isNull()) {
        pix = pix.scaled(100, 100, Qt::KeepAspectRatio, Qt::SmoothTransformation);
        r.translate(10, 10);
        r.setSize(pix.size());
        painter->drawPixmap(r, pix);
    } else {
        QPalette::ColorGroup cg = option.state & QStyle::State_Enabled ? QPalette::Normal : QPalette::Disabled;
        if (option.state & QStyle::State_Selected) {
            painter->setPen(option.palette.color(cg, QPalette::HighlightedText));
        } else {
            painter->setPen(option.palette.color(cg, QPalette::Text));
        }
        r.translate(20, 50);
        painter->drawText(r, tr("Empty file"));
    }
    painter->restore();
}