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 <QtWidgets>
20
21 #include "highscoresmodel.h"
22 #include "buttondelegate.h"
23 #include "settings.h"
24
ButtonDelegate(QWidget * parent)25 ButtonDelegate::ButtonDelegate(QWidget* parent) : QStyledItemDelegate(parent)
26 {
27
28 }
29
GetGamesListModel(const QModelIndex & index) const30 GamesListModel* ButtonDelegate::GetGamesListModel(const QModelIndex &index) const
31 {
32 if (!index.isValid())
33 {
34 return NULL;
35 }
36
37 GamesListModel* glm = qobject_cast<GamesListModel*>(const_cast<QAbstractItemModel*>(index.model()));
38 return glm;
39 }
40
sizeHint(const QStyleOptionViewItem & option,const QModelIndex & index) const41 QSize ButtonDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
42 {
43 return QStyledItemDelegate::sizeHint(option, index);
44 }
45
createEditor(QWidget * parent,const QStyleOptionViewItem & option,const QModelIndex & index) const46 QWidget* ButtonDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
47 {
48 GamesListModel* glm = GetGamesListModel(index);
49
50 if (glm != NULL && index.column() == glm->GetColIx(GamesListModel::COL_DELETE))
51 {
52 ButtonsCell* editor;
53
54 if (glm->GetId() != GamesListModel::MODEL_ID_SAVED)
55 {
56 editor = new ButtonsCell(parent);
57 }
58 else
59 {
60 editor = new ButtonsCell(parent, ButtonsCell::ACTIONID_SAVE);
61 }
62
63 editor->setMouseTracking(true);
64 editor->setAttribute(Qt::WA_Hover, true);
65 editor->setAttribute(Qt::WA_AlwaysShowToolTips, true);
66
67 connect(editor, SIGNAL(CellButtonClickedSignal()), this, SLOT(ButtonClickedSlot()));
68 return editor;
69 }
70 else if (glm != NULL && index.column() == glm->GetColIx(GamesListModel::COL_USERNAME))
71 {
72 QLineEdit* editor = new QLineEdit(parent);
73 editor->setMaxLength(Settings::MAX_LENGTH_USERNAME);
74 #ifdef Q_WS_X11
75 editor->setFrame(false);
76 #endif
77 editor->setAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
78 return editor;
79 }
80 else
81 {
82 return QStyledItemDelegate::createEditor(parent, option, index);
83 }
84 }
85
setEditorData(QWidget * editor,const QModelIndex & index) const86 void ButtonDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
87 {
88 GamesListModel* glm = GetGamesListModel(index);
89 if (glm != NULL && index.column() == glm->GetColIx(GamesListModel::COL_DELETE))
90 {
91 ;
92 }
93 else
94 {
95 QStyledItemDelegate::setEditorData(editor, index);
96 }
97 }
98
setModelData(QWidget * editor,QAbstractItemModel * model,const QModelIndex & index) const99 void ButtonDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex &index) const
100 {
101 if (!index.isValid() || editor == NULL)
102 {
103 return;
104 }
105
106 GamesListModel* glm = GetGamesListModel(index);
107
108 if (glm != NULL && index.column() == glm->GetColIx(GamesListModel::COL_DELETE))
109 {
110 ButtonsCell* editor = qobject_cast<ButtonsCell*>(sender());
111 if (editor != NULL)
112 {
113 glm->setData(index, editor->GetActionId(), Qt::EditRole);
114 }
115 }
116 else
117 {
118 QStyledItemDelegate::setModelData(editor, model, index);
119 }
120 }
121
ButtonClickedSlot()122 void ButtonDelegate::ButtonClickedSlot()
123 {
124 ButtonsCell* editor = qobject_cast<ButtonsCell*>(sender());
125 emit commitData(editor);
126 }
127