1 /* ColorCode, a free MasterMind clone with built in solver
2 * Copyright (C) 2009 Dirk Laebisch
3 * http://www.laebisch.com/
4 *
5 * ColorCode is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * ColorCode is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with ColorCode. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include "highscoresmodel.h"
20 #include "gametableview.h"
21
GameTableView(QWidget * parent)22 GameTableView::GameTableView(QWidget *parent) : QTableView(parent)
23 {
24 setFocusPolicy(Qt::NoFocus);
25
26 mGamesListModel = NULL;
27 mHoverRow = -1;
28
29 setMouseTracking(true);
30 setAttribute(Qt::WA_Hover, true);
31 setAttribute(Qt::WA_AlwaysShowToolTips, true);
32
33 horizontalHeader()->setStretchLastSection(true);
34 verticalHeader()->setSectionResizeMode(QHeaderView::Fixed);
35 verticalHeader()->setDefaultSectionSize(18);
36 verticalHeader()->hide();
37 horizontalHeader()->setFixedHeight(18);
38
39 setGridStyle(Qt::NoPen);
40 setShowGrid(false);
41 setSelectionBehavior(QAbstractItemView::SelectItems);
42 setAlternatingRowColors(true);
43
44 setFrameStyle(QFrame::NoFrame);
45 setFont(QApplication::font());
46 horizontalHeader()->setHighlightSections(false);
47
48 verticalScrollBar()->setTracking(true);
49 connect(verticalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(ScrollSliderChangedSlot(int)));
50 connect(verticalScrollBar(), SIGNAL(sliderReleased()), this, SLOT(ScrollSliderChangedSlot()));
51
52 viewport()->setMouseTracking(true);
53 viewport()->setAttribute(Qt::WA_Hover, true);
54 viewport()->setAttribute(Qt::WA_AlwaysShowToolTips, true);
55
56 viewport()->setFocus();
57 }
58
SetGamesListModel(GamesListModel * glm)59 void GameTableView::SetGamesListModel(GamesListModel* glm)
60 {
61 mGamesListModel = glm;
62 setModel(glm);
63 connect(this, SIGNAL(HoverRowSignal(int)), mGamesListModel, SLOT(HoverRowSlot(int)));
64 }
65
CloseEditorSlot(const QModelIndex & index)66 void GameTableView::CloseEditorSlot(const QModelIndex &index)
67 {
68 if (mGamesListModel != NULL && index.isValid() && index.column() == mGamesListModel->GetColIx(HighScoresModel::COL_USERNAME))
69 {
70 closeEditor(indexWidget(index), QAbstractItemDelegate::SubmitModelCache);
71 }
72 }
73
ScrollSliderChangedSlot(int)74 void GameTableView::ScrollSliderChangedSlot(int)
75 {
76 SetHoverRowByY((mapFromGlobal(QCursor::pos()).y() - horizontalHeader()->height()));
77 }
78
SetHoverRowByY(const int y)79 void GameTableView::SetHoverRowByY(const int y)
80 {
81 int row = rowAt(y);
82 if (row == mHoverRow)
83 {
84 return;
85 }
86
87 mHoverRow = row;
88
89 emit HoverRowSignal(mHoverRow);
90 }
91
mouseMoveEvent(QMouseEvent * e)92 void GameTableView::mouseMoveEvent(QMouseEvent* e)
93 {
94 QTableView::mouseMoveEvent(e);
95 SetHoverRowByY(e->y());
96 }
97
wheelEvent(QWheelEvent * e)98 void GameTableView::wheelEvent(QWheelEvent* e)
99 {
100 QTableView::wheelEvent(e);
101 SetHoverRowByY(e->y());
102 }
103
leaveEvent(QEvent *)104 void GameTableView::leaveEvent(QEvent*)
105 {
106 if (mHoverRow == -1)
107 {
108 return;
109 }
110 mHoverRow = -1;
111 emit HoverRowSignal(mHoverRow);
112 }
113
114