~ubuntu-branches/ubuntu/wily/mupen64plus/wily

« back to all changes in this revision

Viewing changes to main/gui_qt4/mainwidget.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Sven Eckelmann
  • Date: 2011-07-24 14:23:26 UTC
  • mfrom: (10.1.2 experimental)
  • Revision ID: james.westby@ubuntu.com-20110724142326-x9z5qu8j9jecrmod
Tags: 1.99.4+2
* Upload to unstable
* Remove overrides for lintian warning about change to native package
* Update Vcs-* fields to new anonscm.debian.org URLs in debian/control
* Fix spelling of "Flexible" in debian/control (Closes: #633693)
* Mark all targets in debian/rules as phony
* Add some information about the mupen64plus 2.0 vision in debian/NEWS

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2
 
 *   Mupen64plus - wainwidget.cpp                                          *
3
 
 *   Mupen64Plus homepage: http://code.google.com/p/mupen64plus/           *
4
 
 *   Copyright (C) 2008 Slougi                                             *
5
 
 *                                                                         *
6
 
 *   This program is free software; you can redistribute it and/or modify  *
7
 
 *   it under the terms of the GNU General Public License as published by  *
8
 
 *   the Free Software Foundation; either version 2 of the License, or     *
9
 
 *   (at your option) any later version.                                   *
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, write to the                         *
18
 
 *   Free Software Foundation, Inc.,                                       *
19
 
 *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.          *
20
 
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
21
 
 
22
 
#include <QVBoxLayout>
23
 
#include <QTreeView>
24
 
#include <QSortFilterProxyModel>
25
 
#include <QLabel>
26
 
#include <QHeaderView>
27
 
#include <QKeyEvent>
28
 
#include <QApplication>
29
 
#include <Qt>
30
 
#include <QLabel>
31
 
#include <QLineEdit>
32
 
#include <QSettings>
33
 
#include <QMenu>
34
 
#include <QActionGroup>
35
 
#include <QDialog>
36
 
#include <QPushButton>
37
 
 
38
 
#include "mainwidget.h"
39
 
#include "rommodel.h"
40
 
#include "romdelegate.h"
41
 
#include "globals.h"
42
 
#include "rominfodialog.h"
43
 
 
44
 
namespace core {
45
 
    extern "C" {
46
 
        #include "../romcache.h"
47
 
    }
48
 
}
49
 
 
50
 
MainWidget::MainWidget(QWidget* parent)
51
 
    : QWidget(parent)
52
 
    , m_proxyModel(0)
53
 
{
54
 
    setupUi(this);
55
 
 
56
 
    lineEdit->installEventFilter(this);
57
 
 
58
 
    m_proxyModel = new QSortFilterProxyModel(this);
59
 
    m_proxyModel->setSourceModel(RomModel::self());
60
 
    m_proxyModel->setFilterKeyColumn(-1); // search all columns
61
 
    m_proxyModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
62
 
    m_proxyModel->setSortCaseSensitivity(Qt::CaseInsensitive);
63
 
    m_proxyModel->setDynamicSortFilter(true);
64
 
    m_proxyModel->setSortRole(RomModel::Sort);
65
 
 
66
 
    treeView->setModel(m_proxyModel);
67
 
    treeView->sortByColumn(RomModel::GoodName, Qt::AscendingOrder);
68
 
    treeView->header()->resizeSections(QHeaderView::ResizeToContents);
69
 
    treeView->setFocusProxy(lineEdit);
70
 
    treeView->setItemDelegate(new RomDelegate(this));
71
 
 
72
 
    connect(treeView, SIGNAL(customContextMenuRequested(QPoint)),
73
 
            this, SLOT(treeViewContextMenuRequested(QPoint)));
74
 
 
75
 
    QSettings s;
76
 
    QByteArray headerState = s.value("RomBrowserHeadersState").toByteArray();
77
 
    if (!headerState.isEmpty()) {
78
 
        treeView->header()->restoreState(headerState);
79
 
    } else {
80
 
        for (int i = 0; i < treeView->header()->count(); i++) {
81
 
            treeView->header()->hideSection(i);
82
 
        }
83
 
        for (int i = 0; i <= RomModel::LAST_VISIBLE_COLUMN; i++) {
84
 
            treeView->header()->showSection(i);
85
 
        }
86
 
        resizeHeaderSections();
87
 
    }
88
 
 
89
 
    treeView->header()->setContextMenuPolicy(Qt::CustomContextMenu);
90
 
    connect(treeView->header(), SIGNAL(customContextMenuRequested(QPoint)),
91
 
            this, SLOT(headerContextMenuRequested(QPoint)));
92
 
 
93
 
    m_timer.setSingleShot(true);
94
 
 
95
 
    connect(lineEdit, SIGNAL(textChanged(QString)),
96
 
             this, SLOT(lineEditTextChanged()));
97
 
    connect(&m_timer, SIGNAL(timeout()),
98
 
             this, SLOT(filter()));
99
 
    connect(m_proxyModel, SIGNAL(modelReset()),
100
 
             this, SLOT(resizeHeaderSections()));
101
 
    connect(m_proxyModel, SIGNAL(modelReset()),
102
 
             this, SLOT(filter()));
103
 
    connect(m_proxyModel, SIGNAL(dataChanged(QModelIndex, QModelIndex)),
104
 
             this, SLOT(resizeHeaderSections()));
105
 
    connect(m_proxyModel, SIGNAL(layoutChanged()),
106
 
             this, SLOT(resizeHeaderSections()));
107
 
    connect(treeView, SIGNAL(doubleClicked(QModelIndex)),
108
 
             this, SLOT(treeViewDoubleClicked(QModelIndex)));
109
 
 
110
 
    //lineEdit->setFocus();
111
 
    QTimer::singleShot(0, this, SLOT(filter())); // so we emit the base item count
112
 
}
113
 
 
114
 
MainWidget::~MainWidget()
115
 
{
116
 
    QSettings s;
117
 
    QByteArray headerState = treeView->header()->saveState();
118
 
    s.setValue("RomBrowserHeadersState", headerState);
119
 
}
120
 
 
121
 
QModelIndex MainWidget::getRomBrowserIndex()
122
 
{
123
 
    return treeView->currentIndex();
124
 
}
125
 
 
126
 
void MainWidget::showFilter(bool show)
127
 
{
128
 
    label->setVisible(show);
129
 
    lineEdit->setVisible(show);
130
 
    lineEdit->clear();
131
 
}
132
 
 
133
 
void MainWidget::resizeHeaderSections()
134
 
{
135
 
    QString filter = lineEdit->text();
136
 
    m_proxyModel->setFilterFixedString("");
137
 
    treeView->header()->resizeSections(QHeaderView::ResizeToContents);
138
 
    m_proxyModel->setFilterFixedString(filter);
139
 
    emit itemCountChanged(m_proxyModel->rowCount());
140
 
}
141
 
 
142
 
void MainWidget::lineEditTextChanged()
143
 
{
144
 
    if (m_timer.isActive()) {
145
 
        m_timer.stop();
146
 
    }
147
 
    m_timer.start(50);
148
 
}
149
 
 
150
 
void MainWidget::filter()
151
 
{
152
 
    m_proxyModel->setFilterFixedString(lineEdit->text());
153
 
    emit itemCountChanged(m_proxyModel->rowCount());
154
 
}
155
 
 
156
 
void MainWidget::treeViewDoubleClicked(const QModelIndex& index)
157
 
{
158
 
    load(index);
159
 
}
160
 
 
161
 
void MainWidget::headerContextMenuRequested(const QPoint& pos)
162
 
{
163
 
    QHeaderView* header = treeView->header();
164
 
    QAbstractItemModel* model = header->model();
165
 
    Qt::Orientation o = header->orientation();
166
 
 
167
 
    QMenu menu;
168
 
    QActionGroup group(this);
169
 
 
170
 
    group.setExclusive(false);
171
 
    connect(&group, SIGNAL(triggered(QAction*)),
172
 
            this, SLOT(hideHeaderSection(QAction*)));
173
 
 
174
 
    for (int i = 0; i < header->count(); i++) {
175
 
        QString title = model->headerData(i, o, Qt::DisplayRole).toString();
176
 
        QAction* a = menu.addAction(title);
177
 
        a->setCheckable(true);
178
 
        a->setChecked(!header->isSectionHidden(i));
179
 
        a->setData(i);
180
 
        group.addAction(a);
181
 
    }
182
 
 
183
 
    menu.exec(header->mapToGlobal(pos));
184
 
}
185
 
 
186
 
void MainWidget::hideHeaderSection(QAction* a)
187
 
{
188
 
    int section = a->data().toInt();
189
 
    treeView->header()->setSectionHidden(section, !a->isChecked());
190
 
    resizeHeaderSections();
191
 
}
192
 
 
193
 
void MainWidget::treeViewContextMenuRequested(const QPoint& pos)
194
 
{
195
 
    QModelIndex index = treeView->indexAt(pos);
196
 
    if (!index.isValid()) {
197
 
        return;
198
 
    }
199
 
 
200
 
    QMenu m;
201
 
    QAction* loadAction = m.addAction(tr("Load"));
202
 
    loadAction->setIcon(icon("media-playback-start.png"));
203
 
    QAction* propertiesAction = m.addAction(tr("Properties..."));
204
 
    propertiesAction->setIcon(icon("document-properties.png"));
205
 
    m.addSeparator();
206
 
    QAction* refreshAction = m.addAction(tr("Refresh Rom List"));
207
 
    refreshAction->setIcon(icon("view-refresh.png"));
208
 
 
209
 
    QAction* a = m.exec(QCursor::pos());
210
 
    if (a == loadAction) {
211
 
        load(index);
212
 
    } else if (a == propertiesAction) {
213
 
        int row = index.row();
214
 
        RomInfoDialog* d = new RomInfoDialog(this);
215
 
        d->statusLabel->setMax(5);
216
 
        d->flagLabel->setPixmap(index.sibling(row, RomModel::Country).data(Qt::DecorationRole).value<QPixmap>());
217
 
        d->countryLabel->setText(index.sibling(row, RomModel::Country).data().toString());
218
 
        d->goodNameLabel->setText(index.sibling(row, RomModel::GoodName).data().toString());
219
 
        d->statusLabel->setText(index.sibling(row, RomModel::Status).data().toString());
220
 
        d->lineEdit->setText(index.sibling(row, RomModel::UserComments).data().toString());
221
 
        d->fileNameLabel->setText(index.sibling(row, RomModel::FileName).data().toString());
222
 
        d->md5HashLabel->setText(index.sibling(row, RomModel::MD5Hash).data().toString());
223
 
        d->internalNameLabel->setText(index.sibling(row, RomModel::InternalName).data().toString());
224
 
        d->crc1Label->setText(index.sibling(row, RomModel::CRC1).data().toString());
225
 
        d->crc2Label->setText(index.sibling(row, RomModel::CRC2).data().toString());
226
 
        d->saveTypeLabel->setText(index.sibling(row, RomModel::SaveType).data().toString());
227
 
        d->playersLabel->setText(index.sibling(row, RomModel::Players).data().toString());
228
 
        d->sizeLabel->setText(index.sibling(row, RomModel::Size).data().toString());
229
 
        d->compressionLabel->setText(index.sibling(row, RomModel::CompressionType).data().toString());
230
 
        d->imageTypeLabel->setText(index.sibling(row, RomModel::ImageType).data().toString());
231
 
        d->rumbleLabel->setText(index.sibling(row, RomModel::Rumble).data().toString());
232
 
        d->cicChipLabel->setText(index.sibling(row, RomModel::CICChip).data().toString());
233
 
        d->rumbleLabel->setText(index.sibling(row, RomModel::Rumble).data().toString());
234
 
        d->fullPathLabel->setText(index.data(RomModel::FullPath).toString());
235
 
        d->index = m_proxyModel->mapToSource(index);
236
 
        d->show();
237
 
    } else if (a == refreshAction) {
238
 
        core::g_romcache.rcstask = core::RCS_RESCAN;
239
 
    }
240
 
}
241
 
 
242
 
bool MainWidget::eventFilter(QObject* obj, QEvent* event)
243
 
{
244
 
    bool filtered = false;
245
 
 
246
 
    if (obj == lineEdit) {
247
 
        if (event->type() == QEvent::KeyPress) {
248
 
            QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
249
 
            switch(keyEvent->key()) {
250
 
                case Qt::Key_Up:
251
 
                case Qt::Key_Down:
252
 
                case Qt::Key_PageUp:
253
 
                case Qt::Key_PageDown:
254
 
                    QApplication::sendEvent(treeView, keyEvent);
255
 
                    filtered = true;
256
 
                    break;
257
 
                case Qt::Key_Enter:
258
 
                case Qt::Key_Return:
259
 
                    load(treeView->currentIndex());
260
 
                    filtered = true;
261
 
                    break;
262
 
            }
263
 
        }
264
 
    }
265
 
 
266
 
    return filtered;
267
 
}
268
 
 
269
 
void MainWidget::load(const QModelIndex& index)
270
 
{
271
 
    QString filename = index.data(RomModel::FullPath).toString();
272
 
    unsigned int archivefile = index.data(RomModel::ArchiveFile).toUInt();
273
 
    if (!filename.isEmpty()) {
274
 
        emit romDoubleClicked(filename, archivefile);
275
 
    }
276
 
}
277