1
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2
* Mupen64plus - wainwidget.cpp *
3
* Mupen64Plus homepage: http://code.google.com/p/mupen64plus/ *
4
* Copyright (C) 2008 Slougi *
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. *
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. *
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
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
22
#include <QVBoxLayout>
24
#include <QSortFilterProxyModel>
26
#include <QHeaderView>
28
#include <QApplication>
34
#include <QActionGroup>
36
#include <QPushButton>
38
#include "mainwidget.h"
40
#include "romdelegate.h"
42
#include "rominfodialog.h"
46
#include "../romcache.h"
50
MainWidget::MainWidget(QWidget* parent)
56
lineEdit->installEventFilter(this);
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);
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));
72
connect(treeView, SIGNAL(customContextMenuRequested(QPoint)),
73
this, SLOT(treeViewContextMenuRequested(QPoint)));
76
QByteArray headerState = s.value("RomBrowserHeadersState").toByteArray();
77
if (!headerState.isEmpty()) {
78
treeView->header()->restoreState(headerState);
80
for (int i = 0; i < treeView->header()->count(); i++) {
81
treeView->header()->hideSection(i);
83
for (int i = 0; i <= RomModel::LAST_VISIBLE_COLUMN; i++) {
84
treeView->header()->showSection(i);
86
resizeHeaderSections();
89
treeView->header()->setContextMenuPolicy(Qt::CustomContextMenu);
90
connect(treeView->header(), SIGNAL(customContextMenuRequested(QPoint)),
91
this, SLOT(headerContextMenuRequested(QPoint)));
93
m_timer.setSingleShot(true);
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)));
110
//lineEdit->setFocus();
111
QTimer::singleShot(0, this, SLOT(filter())); // so we emit the base item count
114
MainWidget::~MainWidget()
117
QByteArray headerState = treeView->header()->saveState();
118
s.setValue("RomBrowserHeadersState", headerState);
121
QModelIndex MainWidget::getRomBrowserIndex()
123
return treeView->currentIndex();
126
void MainWidget::showFilter(bool show)
128
label->setVisible(show);
129
lineEdit->setVisible(show);
133
void MainWidget::resizeHeaderSections()
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());
142
void MainWidget::lineEditTextChanged()
144
if (m_timer.isActive()) {
150
void MainWidget::filter()
152
m_proxyModel->setFilterFixedString(lineEdit->text());
153
emit itemCountChanged(m_proxyModel->rowCount());
156
void MainWidget::treeViewDoubleClicked(const QModelIndex& index)
161
void MainWidget::headerContextMenuRequested(const QPoint& pos)
163
QHeaderView* header = treeView->header();
164
QAbstractItemModel* model = header->model();
165
Qt::Orientation o = header->orientation();
168
QActionGroup group(this);
170
group.setExclusive(false);
171
connect(&group, SIGNAL(triggered(QAction*)),
172
this, SLOT(hideHeaderSection(QAction*)));
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));
183
menu.exec(header->mapToGlobal(pos));
186
void MainWidget::hideHeaderSection(QAction* a)
188
int section = a->data().toInt();
189
treeView->header()->setSectionHidden(section, !a->isChecked());
190
resizeHeaderSections();
193
void MainWidget::treeViewContextMenuRequested(const QPoint& pos)
195
QModelIndex index = treeView->indexAt(pos);
196
if (!index.isValid()) {
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"));
206
QAction* refreshAction = m.addAction(tr("Refresh Rom List"));
207
refreshAction->setIcon(icon("view-refresh.png"));
209
QAction* a = m.exec(QCursor::pos());
210
if (a == loadAction) {
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);
237
} else if (a == refreshAction) {
238
core::g_romcache.rcstask = core::RCS_RESCAN;
242
bool MainWidget::eventFilter(QObject* obj, QEvent* event)
244
bool filtered = false;
246
if (obj == lineEdit) {
247
if (event->type() == QEvent::KeyPress) {
248
QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
249
switch(keyEvent->key()) {
253
case Qt::Key_PageDown:
254
QApplication::sendEvent(treeView, keyEvent);
259
load(treeView->currentIndex());
269
void MainWidget::load(const QModelIndex& index)
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);