1
/* Qlom is copyright Openismus GmbH, 2009
2
 *
3
 * This file is part of Qlom
4
 *
5
 * Qlom 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
 * Qlom 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 Qlom. If not, see <http://www.gnu.org/licenses/>.
17
 */
18
19
#ifndef QLOM_LAYOUT_DELEGATE_H_
20
#define QLOM_LAYOUT_DELEGATE_H_
21
22
#include <QStyledItemDelegate>
23
#include <QPainter>
24
#include <QStyleOptionViewItem>
25
#include <QModelIndex>
26
#include <QSize>
27
28
#include <libglom/data_structure/layout/fieldformatting.h>
29
#include <libglom/data_structure/field.h>
30
31
/** This is the base class for Glom::FieldFormatting-specific delegates used to
32
  * format the style of Glom::LayoutItems in the view. In principle, each
33
  * Glom::LayoutItem implementing get_formatting_used() (which is not part of
34
  * the interface, though) gets its own delegate class derived from
35
  * GlomFieldFormattingDelegate.
36
  */
37
class QlomFieldFormattingDelegate : public QStyledItemDelegate
38
{
39
    Q_OBJECT
40
41
public:
42
    typedef Glom::sharedptr<const Glom::Field> GlomSharedField;
43
44
    /* Explicit ctor, although it might have been useful for implicit type
45
       conversions. */
46
    explicit QlomFieldFormattingDelegate(
47
        const Glom::FieldFormatting &formatting, const GlomSharedField details,
48
        QObject *parent = 0);
49
    virtual ~QlomFieldFormattingDelegate();
50
51
    virtual void paint(QPainter *painter, const QStyleOptionViewItem &option,
52
        const QModelIndex &index) const;
53
    virtual QSize sizeHint(const QStyleOptionViewItem &option,
54
        const QModelIndex &index) const;
55
56
protected:
57
    const Glom::FieldFormatting theFormattingUsed;
58
    const GlomSharedField theFieldDetails;
59
};
60
61
62
class QlomLayoutItemFieldDelegate : public QlomFieldFormattingDelegate
63
{
64
    Q_OBJECT
65
66
public:
67
    explicit QlomLayoutItemFieldDelegate(
68
        const Glom::FieldFormatting &formatting, const GlomSharedField details,
69
        QObject *parent = 0);
70
    virtual ~QlomLayoutItemFieldDelegate();
71
72
    virtual QString displayText(const QVariant &value, const QLocale &locale)
73
        const;
74
75
private:
76
    QString applyNumericFormatting(double numeric, const QLocale &locale) const;
77
};
78
79
class QlomLayoutItemTextDelegate : public QlomFieldFormattingDelegate
80
{
81
    Q_OBJECT
82
83
public:
84
    QlomLayoutItemTextDelegate(const Glom::FieldFormatting &formatting,
85
        const GlomSharedField details, const QString &displayText, QObject *parent = 0);
86
    virtual ~QlomLayoutItemTextDelegate();
87
88
    /** Ignore the value from the model for static text item and return the
89
     * stored value - theDisplayText - instead.
90
     *  @param[in] unused
91
     *  @param[in] unused
92
     */
93
    virtual QString displayText(const QVariant &, const QLocale &) const;
94
95
private:
96
    QString theDisplayText;
97
};
98
99
/** QSignalMappers only accept a limited subset of types for their bindins, one
100
 *  of them being QObjects. This class acts as a closure for QModelIndices */
101
class QlomModelIndexObject : public QObject
102
{
103
   Q_OBJECT
104
105
public:
106
    explicit QlomModelIndexObject(const QModelIndex &index, QObject *parent = 0);
107
    virtual ~QlomModelIndexObject();
108
109
    QModelIndex index() const;
110
111
private:
112
    QModelIndex theIndex;
113
};
114
115
/** This class inserts a button into the view (at the queried model index)
116
  * when it is *first* asked to paint itself. */
117
class QlomButtonDelegate : public QStyledItemDelegate
118
{
119
    Q_OBJECT
120
121
public:
122
    explicit QlomButtonDelegate(const QString &label, QObject *parent = 0);
123
    virtual ~QlomButtonDelegate();
124
125
    /** This method assumes:
126
      * 1) parent() returns a valid QAbstractItemView,
127
      * 2) There is no QWidget at index yet,
128
      * 3) The widget at index does not get replaced (this is a soft
129
      *    assumption, paint(.) would simply not recreate the button widget in
130
      *    that case). */
131
    virtual void paint(QPainter *painter, const QStyleOptionViewItem &option,
132
        const QModelIndex &index) const;
133
134
Q_SIGNALS:
135
    void buttonPressed(QObject *obj);
136
137
private:
138
    QString theLabel;
139
};
140
#endif // QLOM_LAYOUT_DELEGATE_H_