File: buttondelegate.cpp

package info (click to toggle)
colorcode 0.7.2-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 1,208 kB
  • ctags: 952
  • sloc: cpp: 8,333; makefile: 56
file content (126 lines) | stat: -rw-r--r-- 3,716 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/* ColorCode, a free MasterMind clone with built in solver
 * Copyright (C) 2009  Dirk Laebisch
 * http://www.laebisch.com/
 *
 * ColorCode is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * ColorCode is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with ColorCode. If not, see <http://www.gnu.org/licenses/>.
*/

#include <QtGui>

#include "highscoresmodel.h"
#include "buttondelegate.h"
#include "settings.h"

ButtonDelegate::ButtonDelegate(QWidget* parent) : QStyledItemDelegate(parent)
{

}

GamesListModel* ButtonDelegate::GetGamesListModel(const QModelIndex &index) const
{
    if (!index.isValid())
    {
        return NULL;
    }

    GamesListModel* glm = qobject_cast<GamesListModel*>(const_cast<QAbstractItemModel*>(index.model()));
    return glm;
}

QSize ButtonDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    return QStyledItemDelegate::sizeHint(option, index);
}

QWidget* ButtonDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    GamesListModel* glm = GetGamesListModel(index);

    if (glm != NULL && index.column() == glm->GetColIx(GamesListModel::COL_DELETE))
    {
        ButtonsCell* editor;

        if (glm->GetId() != GamesListModel::MODEL_ID_SAVED)
        {
            editor = new ButtonsCell(parent);
        }
        else
        {
            editor = new ButtonsCell(parent, ButtonsCell::ACTIONID_SAVE);
        }

        editor->setMouseTracking(true);
        editor->setAttribute(Qt::WA_Hover, true);
        editor->setAttribute(Qt::WA_AlwaysShowToolTips, true);

        connect(editor, SIGNAL(CellButtonClickedSignal()), this, SLOT(ButtonClickedSlot()));
        return editor;
    }
    else if (glm != NULL && index.column() == glm->GetColIx(GamesListModel::COL_USERNAME))
    {
        QLineEdit* editor = new QLineEdit(parent);
        editor->setMaxLength(Settings::MAX_LENGTH_USERNAME);
#ifdef Q_WS_X11
        editor->setFrame(false);
#endif
        editor->setAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
        return editor;
    }
    else
    {
        return QStyledItemDelegate::createEditor(parent, option, index);
    }
}

void ButtonDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
    GamesListModel* glm = GetGamesListModel(index);
    if (glm != NULL && index.column() == glm->GetColIx(GamesListModel::COL_DELETE))
    {
        ;
    }
    else
    {
        QStyledItemDelegate::setEditorData(editor, index);
    }
}

void ButtonDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex &index) const
{
    if (!index.isValid() || editor == NULL)
    {
        return;
    }

    GamesListModel* glm = GetGamesListModel(index);

    if (glm != NULL && index.column() == glm->GetColIx(GamesListModel::COL_DELETE))
    {
        ButtonsCell* editor = qobject_cast<ButtonsCell*>(sender());
        if (editor != NULL)
        {
            glm->setData(index, editor->GetActionId(), Qt::EditRole);
        }
    }
    else
    {
        QStyledItemDelegate::setModelData(editor, model, index);
    }
}

void ButtonDelegate::ButtonClickedSlot()
{
    ButtonsCell* editor = qobject_cast<ButtonsCell*>(sender());
    emit commitData(editor);
}