dtkWidgetsMenu.cpp 3.57 KB
Newer Older
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Version: $Id$
//
//

// Commentary:
//
//

// Change Log:
//
//

// Code:

NICLAUSSE Nicolas's avatar
NICLAUSSE Nicolas committed
15
#include "dtkWidgetsMenu.h"
WINTZ Julien's avatar
WINTZ Julien committed
16
#include "dtkWidgetsMenu+ux.h"
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
17
#include "dtkWidgetsMenuItem.h"
NICLAUSSE Nicolas's avatar
NICLAUSSE Nicolas committed
18

19 20 21 22
// ///////////////////////////////////////////////////////////////////
// dtkWidgetsMenuPrivate
// ///////////////////////////////////////////////////////////////////

NICLAUSSE Nicolas's avatar
NICLAUSSE Nicolas committed
23 24 25
class dtkWidgetsMenuPrivate
{
public:
26 27
    QHash<QString, QObject *> object_hash;
    QVector<QObject *>        object_list;
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
28
    QVector<dtkWidgetsMenu *>   menu_list;
29 30 31 32 33 34

public:
    QString title;

public:
    int icon_id;
NICLAUSSE Nicolas's avatar
NICLAUSSE Nicolas committed
35 36
};

KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
37
// ///////////////////////////////////////////////////////////////////
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
38
// dtkWidgetsMenu
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
39
// ///////////////////////////////////////////////////////////////////
NICLAUSSE Nicolas's avatar
NICLAUSSE Nicolas committed
40

41
dtkWidgetsMenu::dtkWidgetsMenu(int icon_id, const QString& title, QObject *parent): QObject(parent), d(new dtkWidgetsMenuPrivate)
NICLAUSSE Nicolas's avatar
NICLAUSSE Nicolas committed
42
{
43 44
    d->title   = title;
    d->icon_id = icon_id;
45

NICLAUSSE Nicolas's avatar
NICLAUSSE Nicolas committed
46 47 48 49
}

dtkWidgetsMenu::~dtkWidgetsMenu(void)
{
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
50
    this->clear();
WINTZ Julien's avatar
WINTZ Julien committed
51

NICLAUSSE Nicolas's avatar
NICLAUSSE Nicolas committed
52 53 54
    delete d;
}

KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
55
dtkWidgetsMenu *dtkWidgetsMenu::addMenu(dtkWidgetsMenu *menu)
NICLAUSSE Nicolas's avatar
NICLAUSSE Nicolas committed
56
{
57 58 59
    if (menu && !d->object_hash.contains(menu->title())) {
        d->object_hash.insert(menu->title(), menu);
        d->object_list << menu;
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
60
        d->menu_list << menu;
61 62
        return menu;
    }
WINTZ Julien's avatar
WINTZ Julien committed
63

64
    return nullptr;
NICLAUSSE Nicolas's avatar
NICLAUSSE Nicolas committed
65 66
}

KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
67
dtkWidgetsMenu *dtkWidgetsMenu::addMenu(int icon_id, const QString& title)
NICLAUSSE Nicolas's avatar
NICLAUSSE Nicolas committed
68
{
69
    dtkWidgetsMenu *menu = new dtkWidgetsMenu(icon_id, title);
WINTZ Julien's avatar
WINTZ Julien committed
70

71
    auto m = this->addMenu(menu);
WINTZ Julien's avatar
WINTZ Julien committed
72

73 74 75
    if (m != menu) {
        delete menu;
    }
WINTZ Julien's avatar
WINTZ Julien committed
76

77
    return m;
NICLAUSSE Nicolas's avatar
NICLAUSSE Nicolas committed
78 79
}

80 81
void dtkWidgetsMenu::clear(void)
{
82 83
    d->object_hash.clear();
    d->object_list.clear();
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
84
    d->menu_list.clear();
85 86
}

KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
87
dtkWidgetsMenuItem *dtkWidgetsMenu::addItem(dtkWidgetsMenuItem *item)
NICLAUSSE Nicolas's avatar
NICLAUSSE Nicolas committed
88
{
89 90 91 92 93
    if (item && !d->object_hash.contains(item->title())) {
        d->object_hash.insert(item->title(), item);
        d->object_list << item;
        return item;
    }
WINTZ Julien's avatar
WINTZ Julien committed
94

95
    return nullptr;
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
96 97 98 99
}

dtkWidgetsMenuItem *dtkWidgetsMenu::addItem(int icon_id, const QString& title)
{
100
    dtkWidgetsMenuItem *item = new dtkWidgetsMenuItem(icon_id, title);
WINTZ Julien's avatar
WINTZ Julien committed
101

102
    auto it = this->addItem(item);
WINTZ Julien's avatar
WINTZ Julien committed
103

104 105 106
    if (it != item) {
        delete item;
    }
WINTZ Julien's avatar
WINTZ Julien committed
107

108
    return it;
NICLAUSSE Nicolas's avatar
NICLAUSSE Nicolas committed
109 110
}

KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
111
dtkWidgetsMenuItem *dtkWidgetsMenu::addSeparator(void)
NICLAUSSE Nicolas's avatar
NICLAUSSE Nicolas committed
112
{
113
    static int count = 0;
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
114

SZPYRKA Jean-Luc's avatar
typo  
SZPYRKA Jean-Luc committed
115
    QString title = QString("separator_%1").arg(count++);
WINTZ Julien's avatar
WINTZ Julien committed
116

117
    dtkWidgetsMenuItem *separator = new dtkWidgetsMenuItem(title, this);
118
    separator->setSeparator(true);
119 120 121

    d->object_hash.insert(title, separator);
    d->object_list << separator;
122

123
    return separator;
NICLAUSSE Nicolas's avatar
NICLAUSSE Nicolas committed
124
}
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
125

126 127 128 129 130
dtkWidgetsMenuItem *dtkWidgetsMenu::addParameter(dtkWidgetsMenuItem *item)
{
    return this->addItem(item);
}

131
dtkWidgetsMenuItem *dtkWidgetsMenu::addParameter(const QString& title, dtkCoreParameter *parameter, const QString& representation)
132 133 134 135 136 137 138 139 140 141 142 143
{
    dtkWidgetsMenuItem *item = new dtkWidgetsMenuItemParameter(title, parameter, representation);

    auto it = this->addItem(item);

    if (it != item) {
        delete item;
    }

    return it;
}

KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
144 145
QString dtkWidgetsMenu::title(void) const
{
146
    return d->title;
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
147 148
}

KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
149 150 151 152 153
QVector<dtkWidgetsMenu *> dtkWidgetsMenu::menus(void) const
{
    return d->menu_list;
}

154 155 156 157 158 159 160 161
dtkWidgetsMenu * dtkWidgetsMenu::menu(const QString& id) const
{
    if(!d->object_hash.contains(id))
        return nullptr;

    return dynamic_cast<dtkWidgetsMenu *>(d->object_hash[id]);
}

WINTZ Julien's avatar
WINTZ Julien committed
162 163
QObject *dtkWidgetsMenu::object(const QString& id)
{
164 165 166
    if(!d->object_hash.contains(id))
        return nullptr;

WINTZ Julien's avatar
WINTZ Julien committed
167 168 169
    return d->object_hash[id];
}

170 171 172 173 174
QVector<QObject *> dtkWidgetsMenu::objects(void) const
{
    return d->object_list;
}

KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
175 176 177 178 179
int dtkWidgetsMenu::icon(void) const
{
    return d->icon_id;
}

KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
180 181
//
// dtkWidgetsMenu.cpp ends here