File: view.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 (105 lines) | stat: -rw-r--r-- 3,425 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
/*
 * view.cpp - plugin
 * Copyright (C) 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 "view.h"
#include "model.h"

#include <QHeaderView>
#include <QMenu>

#include "delegate.h"
#include "iconfactoryaccessinghost.h"

Viewer::Viewer(QWidget *parent) : QTableView(parent) { setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); }

void Viewer::init(IconFactoryAccessingHost *iconHost)
{
    iconHost_ = iconHost;
    setSelectionBehavior(QAbstractItemView::SelectRows);
    setItemDelegateForColumn(3, new IconDelegate(iconHost_, this));
    setItemDelegateForColumn(4, new IconDelegate(iconHost_, this));
    setItemDelegateForColumn(1, new LineEditDelegate(this));
    setItemDelegateForColumn(2, new LineEditDelegate(this));

    QHeaderView *header = horizontalHeader();
    header->setSectionResizeMode(QHeaderView::ResizeToContents);
    verticalHeader()->setDefaultAlignment(Qt::AlignHCenter);

    resizeColumnsToContents();

    connect(this, &Viewer::clicked, this, &Viewer::itemClicked);
}

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

void Viewer::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 Viewer::itemClicked(const QModelIndex &index)
{
    if (index.column() == 0) {
        model()->setData(index, 3); // invert
    } else if (index.column() == 4) {
        emit checkSound(index);
    } else if (index.column() == 3) {
        emit getSound(index);
    }
}

void Viewer::deleteSelected()
{
    QItemSelectionModel *selection = selectionModel();
    qobject_cast<Model *>(model())->deleteRows(selection->selectedRows());
}