~feng-kylin/youker-assistant/youker-assistant

« back to all changes in this revision

Viewing changes to setting/theme/themeview.cpp

  • Committer: lixiang
  • Date: 2018-03-06 03:13:06 UTC
  • Revision ID: lixiang@kylinos.cn-20180306031306-fd7qnru3vm4a1xjd
Rewrite with Qt5, and add system monitor

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2013 ~ 2018 National University of Defense Technology(NUDT) & Tianjin Kylin Ltd.
 
3
 *
 
4
 * Authors:
 
5
 *  Kobe Lee    xiangli@ubuntukylin.com/kobe24_lixiang@126.com
 
6
 *
 
7
 * This program is free software; you can redistribute it and/or modify
 
8
 * it under the terms of the GNU General Public License as published by
 
9
 * the Free Software Foundation; version 3.
 
10
 *
 
11
 * This program is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 * GNU General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License
 
17
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
18
 */
 
19
 
 
20
#include "themeview.h"
 
21
#include "themedelegate.h"
 
22
 
 
23
#include <QApplication>
 
24
#include <QMouseEvent>
 
25
#include <QDebug>
 
26
 
 
27
namespace {
 
28
 
 
29
const QSize THEME_ITEM_DEFAULT_SIZE = QSize(139, 160);
 
30
 
 
31
}
 
32
 
 
33
ThemeView::ThemeView(QWidget *parent)
 
34
    : QListView(parent)
 
35
    , m_itemSize(THEME_ITEM_DEFAULT_SIZE)
 
36
    , m_prevModelIndex(QModelIndex())
 
37
{
 
38
    setMouseTracking(true);
 
39
 
 
40
    m_delegate = new ThemeDelegate(this);
 
41
    setItemDelegate(m_delegate);
 
42
 
 
43
    m_model = new QStandardItemModel(this);
 
44
    setModel(m_model);
 
45
 
 
46
    setEditTriggers(QAbstractItemView::NoEditTriggers);
 
47
    setResizeMode(QListView::Adjust);
 
48
    setViewMode(QListView::IconMode);
 
49
    setSelectionMode(QAbstractItemView::SingleSelection);
 
50
    setUniformItemSizes(true);
 
51
    setSpacing(60);
 
52
    setDragEnabled(false);
 
53
 
 
54
//    connect(this, SIGNAL(ri))
 
55
    connect(this, &ThemeView::doubleClicked, this, &ThemeView::onItemClicked);
 
56
    connect(this, &ThemeView::clicked, this, &ThemeView::onItemClicked);
 
57
    connect(selectionModel(), &QItemSelectionModel::currentChanged, this, [=] (const QModelIndex &current) {
 
58
        if (!current.isValid())
 
59
            return;
 
60
 
 
61
        if (m_prevModelIndex.isValid()) {//update the previous model index's data
 
62
            QList<QVariant> datas = m_prevModelIndex.model()->data(m_prevModelIndex, Qt::DisplayRole).toList();
 
63
            if (!datas.isEmpty()) {
 
64
                datas.replace(3, QVariant(false));
 
65
                m_model->setData(m_prevModelIndex, QVariant(datas), Qt::DisplayRole);
 
66
                m_model->setData(m_prevModelIndex, QVariant(m_itemSize), Qt::SizeHintRole);
 
67
            }
 
68
        }
 
69
 
 
70
        m_prevModelIndex = current;
 
71
    });
 
72
}
 
73
 
 
74
ThemeView::~ThemeView()
 
75
{
 
76
    this->clearData();
 
77
}
 
78
 
 
79
void ThemeView::clearData()
 
80
{
 
81
    setAutoScroll(false);
 
82
    for (int i = 0; i < this->m_model->rowCount(); ++i) {
 
83
        this->m_model->removeRow(i);
 
84
    }
 
85
    setAutoScroll(true);
 
86
    m_prevModelIndex = QModelIndex();
 
87
}
 
88
 
 
89
QModelIndex ThemeView::loadThemeData(const QString &name, bool isCurrrent)
 
90
{
 
91
    QVariantList datas;
 
92
    datas.append(QVariant(name));//theme name
 
93
    datas.append(QVariant(QString(":/gtk/res/theme/%1.png").arg(name)));//theme picture
 
94
    datas.append(QVariant(":/gtk/res/theme/disappear.png"));//default picture
 
95
    datas.append(QVariant(isCurrrent));
 
96
 
 
97
    QModelIndex index;
 
98
    const int existIndex = isExist(name);
 
99
    if (existIndex != -1) {//it exist, then update data
 
100
        index = m_model->index(existIndex, 0);
 
101
    }
 
102
    else {//it not exist, then new item
 
103
        QStandardItem *item = new QStandardItem();
 
104
        QList<QStandardItem *> items;
 
105
        items.append(item);
 
106
        m_model->appendRow(items);
 
107
        index = m_model->index(m_model->rowCount() - 1, 0);
 
108
    }
 
109
 
 
110
    m_model->setData(index, QVariant(datas), Qt::DisplayRole);
 
111
    m_model->setData(index, QVariant(m_itemSize), Qt::SizeHintRole);
 
112
 
 
113
    return index;
 
114
}
 
115
 
 
116
QSize ThemeView::itemSize() const
 
117
{
 
118
    return m_itemSize;
 
119
}
 
120
 
 
121
int ThemeView::isExist(const QString &name) const
 
122
{
 
123
    for (int i = 0; i < m_model->rowCount(); i++) {
 
124
        const QVariantList datas = m_model->data(m_model->index(i, 0), Qt::DisplayRole).toList();
 
125
        if (!datas.isEmpty() && datas[0].toString() == name) {
 
126
            return i;
 
127
        }
 
128
    }
 
129
 
 
130
    return -1;
 
131
}
 
132
 
 
133
void ThemeView::onItemClicked(const QModelIndex &index)
 
134
{
 
135
    if (!index.isValid())
 
136
        return;
 
137
 
 
138
    QList<QVariant> datas = index.model()->data(index, Qt::DisplayRole).toList();
 
139
    if (!datas.isEmpty()) {
 
140
        const QString name = datas[0].toString();
 
141
        const int ti = isExist(name);
 
142
        if (ti != -1) {//it must be exist, then update the current model index's data
 
143
            datas.replace(3, QVariant(true));
 
144
            m_model->setData(index, QVariant(datas), Qt::DisplayRole);
 
145
            m_model->setData(index, QVariant(m_itemSize), Qt::SizeHintRole);
 
146
            setCurrentIndex(index);
 
147
 
 
148
            emit this->sendSelectThemeName(name);
 
149
        }
 
150
    }
 
151
}
 
152
 
 
153
void ThemeView::loadOneTheme(const QString &name, bool isCurrrent)
 
154
{
 
155
    QModelIndex index = loadThemeData(name, isCurrrent);
 
156
    if (isCurrrent)
 
157
        setCurrentIndex(index);
 
158
 
 
159
    scrollTo(m_model->index(index.row() + 1, 0, index.parent()));
 
160
}
 
161
 
 
162
void ThemeView::mousePressEvent(QMouseEvent *e)
 
163
{
 
164
    if (!indexAt(e->pos()).isValid()) {
 
165
        this->selectionModel()->clearSelection();
 
166
    }
 
167
 
 
168
    QListView::mousePressEvent(e);
 
169
}
 
170
 
 
171
/*bool ThemeView::eventFilter(QObject *obj, QEvent *event)
 
172
{
 
173
    if(event->type() == QEvent::MouseButtonPress) {
 
174
        QMouseEvent *me = (QMouseEvent *)event;
 
175
    }
 
176
 
 
177
 
 
178
    if (event->type() == QEvent::KeyPress) {
 
179
        QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
 
180
        qDebug("Key press %d", keyEvent->key());
 
181
        if (keyEvent->key() == Qt::Key_Tab) {
 
182
 
 
183
        }
 
184
        else if (keyEvent->key() == Qt::Key_Escape) {
 
185
 
 
186
        }
 
187
        return true;
 
188
    }
 
189
    else {
 
190
        // standard event processing
 
191
        return QObject::eventFilter(obj, event);
 
192
    }
 
193
}*/