File: StylesCombo.cpp

package info (click to toggle)
calligra 1%3A2.4.4-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 290,028 kB
  • sloc: cpp: 1,105,019; xml: 24,940; ansic: 11,807; python: 8,457; perl: 2,792; sh: 1,507; yacc: 1,307; ruby: 1,248; sql: 903; lex: 455; makefile: 89
file content (183 lines) | stat: -rw-r--r-- 6,416 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/* This file is part of the KDE project
 * Copyright (C) 2011-2012 Pierre Stirnweiss <pstirnweiss@googlemail.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/
#include "StylesCombo.h"
#include <KoStyleThumbnailer.h>

#include "StylesModel.h"
#include "StylesComboPreview.h"
#include "StylesDelegate.h"

#include <QListView>
#include <QMouseEvent>
#include <QStyleOptionViewItemV4>

#include <KDebug>

StylesCombo::StylesCombo(QWidget *parent)
    : QComboBox(parent),
      m_stylesModel(0),
      m_view(new QListView()),
      m_selectedItem(-1),
      m_originalStyle(true)
{
    setMinimumSize(50,32);

    m_view->setMinimumWidth(250);
    m_view->setMouseTracking(true);
    setView(m_view);
    view()->viewport()->installEventFilter(this);

    StylesDelegate *delegate = new StylesDelegate();
    connect(delegate, SIGNAL(needsUpdate(QModelIndex)), m_view, SLOT(update(QModelIndex)));
    connect(delegate, SIGNAL(styleManagerButtonClicked(QModelIndex)), this, SLOT(slotShowDia(QModelIndex)));
    connect(delegate, SIGNAL(deleteStyleButtonClicked(QModelIndex)), this, SLOT(slotDeleteStyle(QModelIndex)));
    connect(delegate, SIGNAL(clickedInItem(QModelIndex)), this, SLOT(slotItemClicked(QModelIndex)));
    setItemDelegate(delegate);

    connect(this, SIGNAL(currentIndexChanged(int)), this, SLOT(slotSelectionChanged(int)));

    QComboBox::setEditable(true);
    setIconSize(QSize(0,0));

    StylesComboPreview *preview = new StylesComboPreview(this);
    QComboBox::setEditable(true);
    setLineEdit(preview);
}

StylesCombo::~StylesCombo()
{
}

void StylesCombo::setStyleIsOriginal(bool original)
{
    m_originalStyle = original;
    if (!original) {
        m_preview->setAddButtonShown(true);
    }
    else {
        m_preview->setAddButtonShown(false);
    }
}

void StylesCombo::setStylesModel(StylesModel *model)
{
    m_stylesModel = model;
    setModel(model);
}

void StylesCombo::setEditable(bool editable)
{
    if (editable) {
        // Create a StylesComboPreview instead of a QLineEdit
        // Compared to QComboBox::setEditable, we might be missing the SH_ComboBox_Popup code though...
        // If a style needs this, then we'll need to call QComboBox::setEditable and then setLineEdit again
        StylesComboPreview *edit = new StylesComboPreview( this );
        setLineEdit( edit );
    } else {
        QComboBox::setEditable(editable);
    }
}

void StylesCombo::setLineEdit(QLineEdit *edit)
{
    if (!isEditable() && edit && !qstrcmp(edit->metaObject()->className(), "QLineEdit")) {
        // uic generates code that creates a read-only StylesCombo and then
        // calls combo->setEditable( true ), which causes QComboBox to set up
        // a dumb QLineEdit instead of our nice StylesComboPreview.
        // As some StylesCombo features rely on the StylesComboPreview, we reject
        // this order here.
        delete edit;
        StylesComboPreview* preview = new StylesComboPreview(this);
        edit = preview;
    }

    QComboBox::setLineEdit(edit);
    m_preview = qobject_cast<StylesComboPreview*>(edit);

    if (m_preview) {
        connect(m_preview, SIGNAL(resized()), this, SLOT(slotUpdatePreview()));
        connect(m_preview, SIGNAL(newStyleRequested(QString)), this, SIGNAL(newStyleRequested(QString)));
        connect(m_preview, SIGNAL(clicked()), this, SLOT(slotPreviewClicked()));
    }

}

void StylesCombo::slotSelectionChanged(int index)
{
    m_selectedItem = index;
    m_preview->setPreview(m_stylesModel->stylePreview(index, m_preview->availableSize()));
    update();
    emit selectionChanged(index);
}

void StylesCombo::slotItemClicked(QModelIndex index)
{
    //this slot allows us to emit a selected signal. There is a bit of redundancy if the item clicked was indeed a new selection, wher we also emit the selectionChanged signal from the slot above.
    m_selectedItem = index.row();
    m_preview->setPreview(m_stylesModel->stylePreview(m_selectedItem, m_preview->availableSize()));
    update();
    emit selected(m_selectedItem);
}

void StylesCombo::slotUpdatePreview()
{
    m_preview->setPreview(m_stylesModel->stylePreview(currentIndex(), m_preview->availableSize()));
    update();
}

void StylesCombo::slotPreviewClicked()
{
    if (!view()->isVisible()) {
        showPopup();
    }
}

bool StylesCombo::eventFilter(QObject *object, QEvent *event)
{
    if (event->type() == QEvent::MouseButtonRelease && object == view()->viewport()) {
        QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
        //If what follows isn't a HACK then I have no clue what is!!!!
        //The item delegate editorEvent method is not sent MouseButtonRelease events.
        //This is because the QComboBox installs an event filter on the view and calls
        //popup->hide() on MouseButtonRelease to dismiss the view. Since we installed an event filter on the view
        //ourselves, we can prevent hiding the popup. We have to call itemDelegate->editorEvent
        //manually though.
        QModelIndex index = view()->indexAt(mouseEvent->pos());
        QModelIndex buddy = m_stylesModel->buddy(index);
        QStyleOptionViewItemV4 options;
        options.rect = view()->visualRect(buddy);
        options.widget = m_view;
        options.state |= (buddy == view()->currentIndex() ? QStyle::State_HasFocus : QStyle::State_None);
        return view()->itemDelegate()->editorEvent(mouseEvent, m_stylesModel, options, index);
    }
    return false;
}

void StylesCombo::slotShowDia(QModelIndex index)
{
    emit showStyleManager(index.row());
}

void StylesCombo::slotDeleteStyle(QModelIndex index)
{
    emit deleteStyle(index.row());
}

#include <StylesCombo.moc>