basicwidgets.cpp 32.4 KB
Newer Older
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
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/

#include "basicwidgets.h"
#include <qlayoutobject.h>
#include <private/graphicswidgets_p.h>
#include <qmlcontext.h>
#include <qmlengine.h>
#include <qmlcomponent.h>
#include <QtCore/QDebug>
#include <QFile>
#include <QPixmap>
#include <QTimeLine>
#include <QFileInfo>
#include <QMenu>
#include <QAction>
#include <QListView>
#include <QDebug>
#include <QApplication>
#include <QGraphicsOpacityEffect>


QT_BEGIN_NAMESPACE


class QWidgetDeclarativeUI;

class ResizeEventFilter : public QObject
{
    Q_OBJECT
public:
    ResizeEventFilter(QObject *parent) : QObject(parent), m_target(0) { }

    void setTarget(QObject *target) { m_target = target; }
    void setDuiTarget(QWidgetDeclarativeUI* dui_target) {m_dui_target = dui_target;}

protected:
    bool eventFilter(QObject *obj, QEvent *event);
private:
    QObject *m_target;
    QWidgetDeclarativeUI* m_dui_target;
};

class QWidgetDeclarativeUI : public QObject
{
    Q_OBJECT

74
    Q_PROPERTY(QmlListProperty<QObject> children READ children)
75
    Q_PROPERTY(QLayoutObject *layout READ layout WRITE setLayout)
76
    Q_PROPERTY(QmlListProperty<Action> actions READ actions)
77
78
79
80
81
82
83
    Q_PROPERTY(QFont font READ font CONSTANT)

    Q_PROPERTY(QPoint pos READ pos)
    Q_PROPERTY(QSize size READ size)

    Q_PROPERTY(int x READ x WRITE setX NOTIFY xChanged)
    Q_PROPERTY(int y READ y WRITE setY NOTIFY yChanged)
84
85
86
87

    Q_PROPERTY(int globalX READ globalX WRITE setGlobalX)
    Q_PROPERTY(int globalY READ globalY WRITE setGlobalY)

88
89
90
91
92
93
94
95
96
97
98
99
100
    Q_PROPERTY(bool focus READ hasFocus NOTIFY focusChanged)
    Q_PROPERTY(int width READ width WRITE setWidth NOTIFY widthChanged)
    Q_PROPERTY(int height READ height WRITE setHeight NOTIFY heightChanged)
    Q_PROPERTY(qreal opacity READ opacity WRITE setOpacity NOTIFY opacityChanged)

    Q_PROPERTY(QUrl styleSheetFile READ styleSheetFile WRITE setStyleSheetFile NOTIFY styleSheetFileChanged)

    Q_PROPERTY(QColor windowColor READ windowColor WRITE setWindowColor)
    Q_PROPERTY(QColor backgroundColor READ backgroundColor WRITE setBackgroundColor)
    Q_PROPERTY(QColor baseColor READ baseColor WRITE setBaseColor)
    Q_PROPERTY(QColor textColor READ textColor WRITE setTextColor)
    Q_PROPERTY(QColor windowTextColor READ windowTextColor WRITE setWindowTextColor)
    Q_PROPERTY(QColor buttonTextColor READ buttonTextColor WRITE setButtonTextColor)
101
    Q_PROPERTY(QColor buttonColor READ buttonColor WRITE setButtonColor)
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119

    Q_PROPERTY(int fixedWidth READ width WRITE setFixedWidth)
    Q_PROPERTY(int fixedHeight READ height WRITE setFixedHeight)

    Q_PROPERTY(bool mouseOver READ mouseOver NOTIFY mouseOverChanged)

    Q_CLASSINFO("DefaultProperty", "children")

signals:
    void xChanged();
    void yChanged();
    void widthChanged();
    void heightChanged();
    void focusChanged();
    void mouseOverChanged();
    void opacityChanged();

public:
120
    QWidgetDeclarativeUI(QObject *other) : QObject(other), _layout(0), _graphicsOpacityEffect(0) {
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
        q = qobject_cast<QWidget*>(other);
        ResizeEventFilter *filter(new ResizeEventFilter(q));
        filter->setTarget(q);
        filter->setDuiTarget(this);
        m_mouseOver = false;
        q->installEventFilter(filter);
        Q_ASSERT(q);
    }
    virtual ~QWidgetDeclarativeUI() {
    }

public:

    void setMouseOver(bool _mouseOver)
    {
        m_mouseOver = _mouseOver;
    }

    void emitResize()
    {
        emit widthChanged();
        emit heightChanged();
    }

    void emitMove()
    {
        emit xChanged();
        emit yChanged();
    }

    void emitFocusChanged()
    {
        emit focusChanged();
    }

     void emitMouseOverChanged()
    {
        emit mouseOverChanged();
    }

161
162
163
    QmlListProperty<QObject> children() {
        return QmlListProperty<QObject>(this, 0, children_append, children_count, children_at, children_clear);
    }
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188

    QLayoutObject *layout() const { return _layout; }
    void setLayout(QLayoutObject *lo)
    {
        _layout = lo;
        static_cast<QWidget *>(parent())->setLayout(lo->layout());
    }

    QFont font() const
    {
        return _font;
    }

    void setFont(const QFont &font)
    {
        if (font != _font) {
            _font = font;
            static_cast<QWidget *>(parent())->setFont(_font);
        }
    }

    int x() const {
        return q->x();
    }

189
190
191
192
193
194
195
    int globalX() const {
        if (q->parentWidget())
            return q->mapToGlobal(QPoint(x(), 0)).x();
        else
            return x();
    }

196
197
198
199
200
201
202
203
204
205
206
207
    bool hasFocus() const {
        return q->hasFocus();
    }

    bool mouseOver() const {
        return m_mouseOver;
    }

    void setX(int x) {
        q->move(x, y());
    }

208
209
210
211
212
213
214
    void setGlobalX(int newX) {
        if (q->parentWidget())
            setX(q->mapFromGlobal(QPoint(newX, 1)).x());
        else
            setX(newX);
    }

215
216
217
218
    int y() const {
        return q->y();
    }

219
220
221
222
223
224
    int globalY() const {
        if (q->parentWidget())
            return q->mapToGlobal(QPoint(1,y())).y();
        else return y();
    }

225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
    qreal opacity() const {
        if (_graphicsOpacityEffect)
            return _graphicsOpacityEffect->opacity();
        else
            return 1;
    }

    void setOpacity(qreal newOpacity) {
        if (newOpacity != opacity()) {
            if (!_graphicsOpacityEffect) {
                _graphicsOpacityEffect = new QGraphicsOpacityEffect(this);
                q->setGraphicsEffect(_graphicsOpacityEffect);
            }
            _graphicsOpacityEffect->setOpacity(newOpacity);
            emit opacityChanged();
        }
    }

    void setY(int y) {
        q->move(x(), y);
    }

247
248
249
250
251
252
253
    void setGlobalY(int newY) {
        if (q->parentWidget())
            setY(q->parentWidget()->mapFromGlobal(QPoint(1, newY)).y());
        else
            setY(newY);
    }

254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
    int width() const {
        return q->width();
    }

    void setWidth(int width) {
        q->resize(width, height());
    }

    int height() const {
        return q->height();
    }

    void setHeight(int height) {
        q->resize(width(), height);
    }

    void setFixedWidth(int width) {
        q->setFixedWidth(width);
    }

    void setFixedHeight(int height) {
        q->setFixedHeight(height);
    }

    QPoint pos() const {
        return q->pos();
    }

    QSize size() const {
        return q->size();
    }

    QUrl styleSheetFile() const {
        return _styleSheetFile;
    }

    QColor windowColor() const
    { return q->palette().color(QPalette::Window); }

    void setWindowColor(const QColor &color)
    {
        QPalette pal = q->palette();
        pal.setColor(QPalette::Window, color);
        q->setPalette(pal);
    }

    QColor backgroundColor() const
    { return q->palette().color(QPalette::Background); }

    void setBackgroundColor(const QColor &color)
    {
        QPalette pal = q->palette();
        pal.setColor(QPalette::Background, color);
        q->setPalette(pal);
    }

    QColor baseColor() const
    { return q->palette().color(QPalette::Base); }

    void setBaseColor(const QColor &color)
    {
        QPalette pal = q->palette();
        pal.setColor(QPalette::Base, color);
        q->setPalette(pal);
    }

    QColor textColor() const
    { return q->palette().color(QPalette::Text); }

    void setTextColor(const QColor &color)
    {
        QPalette pal = q->palette();
        pal.setColor(QPalette::Text, color);
        q->setPalette(pal);
    }

    QColor windowTextColor() const
    { return q->palette().color(QPalette::WindowText); }

    void setWindowTextColor(const QColor &color)
    {
        QPalette pal = q->palette();
        pal.setColor(QPalette::WindowText, color);
        q->setPalette(pal);
    }

    QColor buttonTextColor() const
    { return q->palette().color(QPalette::ButtonText); }

    void setButtonTextColor(const QColor &color)
    {
        QPalette pal = q->palette();
        pal.setColor(QPalette::ButtonText, color);
        q->setPalette(pal);
    }

350
351
352
353
354
355
356
357
358
359
360
    QColor buttonColor() const
    { return q->palette().color(QPalette::Button); }

    void setButtonColor(const QColor &color)
    {
        QPalette pal = q->palette();
        pal.setColor(QPalette::Button, color);
        q->setPalette(pal);
    }


361
362
363
364
365
    void setStyleSheetFile(const QUrl &url) {
        _styleSheetFile = url;
        _styleSheetFile.setScheme("file"); //### todo
        QString fileName;
        if (!QFileInfo(_styleSheetFile.toLocalFile()).exists())
366
            fileName = (QLatin1Char(':') + _styleSheetFile.toLocalFile().split(QLatin1Char(':')).last()); //try if it is a resource
367
368
369
370
371
372
373
374
375
376
377
378
379
        else
            fileName = (_styleSheetFile.toLocalFile());
        QFile file(fileName);
        file.open(QIODevice::ReadOnly);
        if (file.isOpen()) {
            QString styleSheet(file.readAll());
            q->setStyleSheet(styleSheet);
        } else {
            qWarning() << QLatin1String("setStyleSheetFile: ") << url << QLatin1String(" not found!");
        }

    }

380
381
382
    QmlListProperty<Action> actions() {
        return QmlListProperty<Action>(this, 0, actions_append, actions_count, actions_at, actions_clear);
    }
383

384
385
386
387
388
389
390
private:
    QWidget *q;
    QLayoutObject *_layout;
    QFont _font;
    QUrl _styleSheetFile;
    QGraphicsOpacityEffect *_graphicsOpacityEffect;
    bool m_mouseOver;
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446

    static void children_append(QmlListProperty<QObject> *property, QObject *o) {
        QWidgetDeclarativeUI *p = static_cast<QWidgetDeclarativeUI *>(property->object);
        QWidget *q = p->q;
        if (QWidget *w = qobject_cast<QWidget *>(o))
            w->setParent(static_cast<QWidget *>(q));
        else
            o->setParent(q);
    }

    static int children_count(QmlListProperty<QObject> *property) {
        QWidgetDeclarativeUI *p = static_cast<QWidgetDeclarativeUI *>(property->object);
        QWidget *q = p->q;
        return q->children().count();
    }

    static QObject * children_at(QmlListProperty<QObject> *property, int index) {
        QWidgetDeclarativeUI *p = static_cast<QWidgetDeclarativeUI *>(property->object);
        QWidget *q = p->q;
        return q->children().at(index);
    }

    static void children_clear(QmlListProperty<QObject> *property) {
        QWidgetDeclarativeUI *p = static_cast<QWidgetDeclarativeUI *>(property->object);
        QWidget *q = p->q;
        QObjectList c = q->children();
        for (int i = 0; i < c.count(); ++i)
            c.at(i)->setParent(0);
    }

    // ### Original had an insert, and removeAt
    static void actions_append(QmlListProperty<Action> *property, Action *o) {
        QWidgetDeclarativeUI *p = static_cast<QWidgetDeclarativeUI *>(property->object);
        QWidget *w = p->q;
        o->setParent(w);
        w->addAction(o);
    }

    static int actions_count(QmlListProperty<Action> *property) {
        QWidgetDeclarativeUI *p = static_cast<QWidgetDeclarativeUI *>(property->object);
        QWidget *w = p->q;
        return w->actions().count();
    }
    static Action *actions_at(QmlListProperty<Action> *property, int index) {
        QWidgetDeclarativeUI *p = static_cast<QWidgetDeclarativeUI *>(property->object);
        QWidget *w = p->q;
        return qobject_cast<Action *>(w->actions().at(index));
    }

    static void actions_clear(QmlListProperty<Action> *property) {
        QWidgetDeclarativeUI *p = static_cast<QWidgetDeclarativeUI *>(property->object);
        QWidget *w = p->q;

        while (!w->actions().empty())
            w->removeAction(w->actions().first());
    }
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
};

bool ResizeEventFilter::eventFilter(QObject *obj, QEvent *event)
{
    if (event->type() == QEvent::Resize) {
        if (obj
            && obj->isWidgetType()
            && obj == m_target) {
                m_dui_target->emitResize();
                return QObject::eventFilter(obj, event);
        }
    } else if (event->type() == QEvent::Move) {
        if (obj
            && obj->isWidgetType()
            && obj == m_target) {
                m_dui_target->emitMove();
                return QObject::eventFilter(obj, event);
        }
    } else if ((event->type() == QEvent::FocusIn) || (event->type() == QEvent::FocusOut)) {
        if (obj
            && obj->isWidgetType()
            && obj == m_target) {
                m_dui_target->emitFocusChanged();
                return QObject::eventFilter(obj, event);
        }
    } else if ((event->type() == QEvent::Enter) || (event->type() == QEvent::Leave)) {
        if (obj
            && obj->isWidgetType()
            && obj == m_target) {
                m_dui_target->setMouseOver(event->type() == QEvent::Enter);
                m_dui_target->emitMouseOverChanged();
                return QObject::eventFilter(obj, event);
        }
    }
    return QObject::eventFilter(obj, event);
}


class QTabObject : public QObject
{
    Q_OBJECT

    Q_PROPERTY(QWidget *content READ content WRITE setContent)
    Q_PROPERTY(QString label READ label WRITE setLabel)
    Q_PROPERTY(QIcon icon READ icon WRITE setIcon)
    Q_CLASSINFO("DefaultProperty", "content")
public:
    QTabObject(QObject *parent = 0) : QObject(parent), _content(0) {}

    QWidget *content() const { return _content; }
    void setContent(QWidget *content)
    {
        _content = content;
    }

    QString label() const { return _label; }
    void setLabel(const QString &label)
    {
        _label = label;
    }

    QIcon icon() const { return _icon; }
    void setIcon(const QIcon &icon)
    {
        _icon = icon;
    }

private:
    QWidget *_content;
    QString _label;
    QIcon _icon;
};

class QPushButtonDeclarativeUI : public QObject
{
    Q_OBJECT

    Q_PROPERTY(QUrl iconFromFile READ iconFromFile WRITE setIconFromFile)
public:
    QPushButtonDeclarativeUI(QObject *parent = 0) : QObject(parent)
     {
         pb = qobject_cast<QPushButton*>(parent);
     }
private:
    QUrl iconFromFile() const
    {
        return _url;
    }

    void setIconFromFile(const QUrl &url) {
        _url = url;

        QString path;
        if (_url.scheme() == QLatin1String("qrc")) {
541
            path = QLatin1Char(':') + _url.path();
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
        } else {
            path = _url.toLocalFile();
        }

        QFile file(path);
        file.open(QIODevice::ReadOnly);
        if (file.exists() && file.isOpen()) {
            QPixmap pixmap(path);
            if (pixmap.isNull())
                qWarning() << QLatin1String("setIconFromFile: ") << url << QLatin1String(" not found!");
            pb->setIcon(pixmap);
        } else {
            qWarning() << QLatin1String("setIconFromFile: ") << url << QLatin1String(" not found!");
        }

    }

    QPushButton *pb;
    QUrl _url;
};

563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606

class QLabelDeclarativeUI : public QObject
{
    Q_OBJECT

    Q_PROPERTY(QUrl iconFromFile READ iconFromFile WRITE setIconFromFile)
public:
    QLabelDeclarativeUI(QObject *parent = 0) : QObject(parent)
     {
         lb = qobject_cast<QLabel*>(parent);
     }
private:
    QUrl iconFromFile() const
    {
        return _url;
    }

    void setIconFromFile(const QUrl &url) {
        _url = url;

        QString path;
        if (_url.scheme() == QLatin1String("qrc")) {
            path = QLatin1Char(':') + _url.path();
        } else {
            path = _url.toLocalFile();
        }

        QFile file(path);
        file.open(QIODevice::ReadOnly);
        if (file.exists() && file.isOpen()) {
            QPixmap pixmap(path);
            if (pixmap.isNull())
                qWarning() << QLatin1String("setIconFromFile: ") << url << QLatin1String(" not found!");
            lb->setPixmap(pixmap);
        } else {
            qWarning() << QLatin1String("setIconFromFile: ") << url << QLatin1String(" not found!");
        }

    }

    QLabel *lb;
    QUrl _url;
};

607
608
609
610
611
612
613
class QMenuDeclarativeUI : public QObject
{
    Q_OBJECT

    Q_CLASSINFO("DefaultProperty", "actions")

public:
614
    QMenuDeclarativeUI(QObject *parent = 0) : QObject(parent)
615
616
617
618
619
620
621
622
     {
         menu = qobject_cast<QMenu*>(parent);
     }

private:
    //if not for the at() function, we could use QmlList instead

    QMenu *menu;
623

624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
};

class QToolButtonDeclarativeUI : public QObject
{
    Q_OBJECT

    Q_PROPERTY(QUrl iconFromFile READ iconFromFile WRITE setIconFromFile)
    Q_PROPERTY(QMenu *menu READ menu WRITE setMenu)

public:
    QToolButtonDeclarativeUI(QObject *parent = 0) : QObject(parent)
     {
         pb = qobject_cast<QToolButton*>(parent);
     }
private:

    QMenu *menu() const { return pb->menu(); }
    void setMenu(QMenu *menu)
    {
643
        menu->setParent(0);
644
        pb->setMenu(menu);
645
        menu->setParent(QApplication::topLevelWidgets().first());
646
647
648
649
650
651
652
653
654
655
656
657
    }

    QUrl iconFromFile() const
    {
        return _url;
    }

    void setIconFromFile(const QUrl &url) {
        _url = url;

        QString path;
        if (_url.scheme() == QLatin1String("qrc")) {
658
            path = QLatin1Char(':') + _url.path();
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
        } else {
            path = _url.toLocalFile();
        }

        QFile file(path);
        file.open(QIODevice::ReadOnly);
        if (file.exists() && file.isOpen()) {
            QPixmap pixmap(path);
            if (pixmap.isNull())
                qWarning() << QLatin1String("setIconFromFile: ") << url << QLatin1String(" not found!");
            pb->setIcon(pixmap);
        } else {
            qWarning() << QLatin1String("setIconFromFile: ") << url << QLatin1String(" not found!");
        }

    }

    QToolButton *pb;
    QUrl _url;
};

class QComboBoxDeclarativeUI : public QObject
{
     Q_OBJECT
     Q_PROPERTY(QStringList items READ items WRITE setItems NOTIFY itemChanged)
     Q_PROPERTY(QString currentText READ currentText WRITE setCurrentText NOTIFY currentTextChanged)

public:
     QComboBoxDeclarativeUI(QObject *parent = 0) : QObject(parent), m_itemsSet(false)
     {
         cb = qobject_cast<QComboBox*>(parent);
         connect(cb, SIGNAL(currentIndexChanged(const QString &)), this, SLOT(setCurrentText(const QString &)));
     }

     QString currentText() const
     {
         return cb->currentText();
     }

public slots:
     void setCurrentText(const QString &text)
     {
         if (!m_itemsSet)
             return;
         int i = cb->findText(text);
         if (i  != -1) {
             cb->setCurrentIndex(i);
             emit currentTextChanged();
         }
     }
signals:
    void currentTextChanged();
    void itemsChanged();

private:
    void setItems(const QStringList &list)
    {
        _items = list;
        cb->clear();
        cb->addItems(list);
        m_itemsSet = true;
        emit itemsChanged();
    }

    QStringList items() const
    {
        return _items;
    }

    QComboBox *cb;
    QStringList _items;

    bool m_itemsSet;
};

class QScrollAreaDeclarativeUI : public QObject
{
     Q_OBJECT
     Q_PROPERTY(QWidget *content READ content WRITE setContent)
     Q_CLASSINFO("DefaultProperty", "content")

public slots:
    void setupProperWheelBehaviour();

public:
     QScrollAreaDeclarativeUI(QObject *parent = 0) : QObject(parent), _content(0)
     {
         sa = qobject_cast<QScrollArea*>(parent);
     }

private:
    QWidget *content() const { return _content; }
    void setContent(QWidget *content)
    {
        _content = content;
        sa->setWidget(content);
        sa->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
        sa->verticalScrollBar()->show();
        setupProperWheelBehaviour();
    }

    QWidget *_content;
    QScrollArea *sa;
};

class MouseWheelFilter : public QObject
{
    Q_OBJECT
public:
    MouseWheelFilter(QObject *parent) : QObject(parent), m_target(0) { }

    void setTarget(QObject *target) { m_target = target; }

protected:
    bool eventFilter(QObject *obj, QEvent *event);
private:
    QObject *m_target;
};

bool MouseWheelFilter::eventFilter(QObject *obj, QEvent *event)
{
    if (event->type() == QEvent::Wheel) {
        if (obj
            && obj->isWidgetType()
            && obj != m_target) {
                QApplication::sendEvent(m_target, event);
                return true;
        }
    }
    return QObject::eventFilter(obj, event);
}

void QScrollAreaDeclarativeUI::setupProperWheelBehaviour()
{
// We install here an eventfilter to avoid that scrolling in
// in the ScrollArea changes values in editor widgets
    if (_content) {
        MouseWheelFilter *forwarder(new MouseWheelFilter(this));
        forwarder->setTarget(_content);

        foreach (QWidget *childWidget, _content->findChildren<QWidget*>()) {
            childWidget->installEventFilter(forwarder);
        }
    }
}

class WidgetLoader : public QWidget
{
    Q_OBJECT
    Q_PROPERTY(QString sourceString READ sourceString WRITE setSourceString NOTIFY sourceChanged)
    Q_PROPERTY(QUrl source READ source WRITE setSource NOTIFY sourceChanged)
    Q_PROPERTY(QWidget *widget READ widget NOTIFY widgetChanged)
    Q_PROPERTY(QmlComponent *component READ component NOTIFY sourceChanged)

public:
    WidgetLoader(QWidget * parent = 0) : QWidget(parent), m_source(QUrl()), m_widget(0),
                                         m_component(0), m_layout(0)
    {
        m_layout = new QVBoxLayout(this);
        m_layout->setContentsMargins(0,0,0,0);
    }

    QUrl source() const;
    void setSource(const QUrl &);

    QString sourceString() const
    { return m_source.toString(); }
    void setSourceString(const QString &url)
    { setSource(QUrl(url)); }

    QWidget *widget() const;
    QmlComponent *component() const
    { return m_component; }

signals:
    void widgetChanged();
    void sourceChanged();

private:
    QUrl m_source;
    QWidget *m_widget;
    QmlComponent *m_component;
    QVBoxLayout *m_layout;
842
    QHash<QString, QWidget*> m_cachedWidgets;
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
};

QUrl WidgetLoader::source() const
{
    return m_source;
}

void WidgetLoader::setSource(const QUrl &source)
{
    if (m_source == source)
        return;

    if (m_component) {
        delete m_component;
        m_component = 0;
    }

Marco Bubke's avatar
Marco Bubke committed
860
//    QWidget *oldWidget = m_widget;
861
862
863

    if (m_widget) {
        //m_widget->deleteLater();
864
        m_widget->hide();
865
866
867
868
869
870
871
872
873
874
        m_widget = 0;
    }

    m_source = source;
    if (m_source.isEmpty()) {
        emit sourceChanged();
        emit widgetChanged();
        return;
    }

875
876
877
878
879
    if (m_cachedWidgets.contains(source.toString())) {
        m_widget = m_cachedWidgets.value(source.toString());
        m_widget->show();
    } else {
        m_component = new QmlComponent(qmlEngine(this), m_source, this);
880

881
        if (m_component) {
882
            emit sourceChanged();
883
            emit widgetChanged();
884

885
886
            while (m_component->isLoading())
                QApplication::processEvents();
887

888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
            if (!m_component->isReady()) {
                if (!m_component->errors().isEmpty())
                    qWarning() << m_component->errors();
                emit sourceChanged();
                return;
            }

            QmlContext *ctxt = new QmlContext(qmlContext(this));
            ctxt->addDefaultObject(this);

            QObject *obj = m_component->create(ctxt);
            if (obj) {
                QWidget *widget = qobject_cast<QWidget *>(obj);
                if (widget) {
                    m_cachedWidgets.insert(source.toString(), widget);
                    m_widget = widget;
                    m_layout->addWidget(m_widget);
                    m_widget->show();
                }
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
            }
        }
    }
}

QWidget *WidgetLoader::widget() const
{
    return m_widget;
}

class MyGroupBox : public QGroupBox
{
     Q_OBJECT

public:
922
    MyGroupBox(QWidget * parent = 0) : QGroupBox(parent), m_animated(false), m_firstExpand(true)
923
924
925
926
927
928
929
930
    {}

void setPixmap(const QPixmap &pixmap, qreal alpha = 1)
{ m_pixmap = pixmap; m_alpha = alpha;}

void setAnimated(bool animated)
{ m_animated = animated; }

931
932
933
void finishFirstExpand()
{ m_firstExpand = false; }

934
935
936
937
938
939
940
941
942
public slots:
    virtual void setVisible ( bool visible );

protected:
    virtual void paintEvent(QPaintEvent * event);
private:
    qreal m_alpha;
    QPixmap m_pixmap;
    bool m_animated;
943
    bool m_firstExpand;
944
945
946
947
948
949
950
951
};

void MyGroupBox::paintEvent(QPaintEvent * event)
{
    QGroupBox::paintEvent(event);
    if (m_animated) {
        QPainter p(this);
        p.setOpacity(m_alpha);
952
953
        if (!m_pixmap.isNull() && !m_firstExpand)
            p.drawPixmap(5, 5,  m_pixmap.width(), m_pixmap.height(), m_pixmap);
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
    }
}

void MyGroupBox::setVisible ( bool visible )
{
    if (parentWidget())
        QGroupBox::setVisible(visible);
}

class QGroupBoxDeclarativeUI : public QObject
{
    Q_OBJECT

    Q_PROPERTY(bool collapsed READ isCollapsed WRITE setCollapsed)
public:
    QGroupBoxDeclarativeUI(QObject *parent = 0) : QObject(parent), m_expanded(true)
    {
        gb =  qobject_cast<MyGroupBox*>(parent);
        connect(&m_timeLine, SIGNAL(frameChanged(int)), this, SLOT(animate(int)));
        connect(&m_timeLine, SIGNAL(finished()), this, SLOT(finish()));

        m_timeLine.setDuration(150);
        m_timeLine.setFrameRange(0, 5);
    }

    bool isCollapsed()
    { return ! m_expanded; }

    void setCollapsed(bool collapsed)
    {
        if (collapsed)
            collapse();
        else
            expand();
    }

public slots:
    void collapse();
    void expand();

    void animate(int frame);
    void finish();

private:
    MyGroupBox *gb;
    QTimeLine m_timeLine;
    bool m_expanded;
    int m_oldHeight;
    int m_oldMAxHeight;
1003
    int m_oldMinHeight;
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
    QPixmap m_contens;

    void hideChildren();
    void showChildren();

    void reLayout();
};

void QGroupBoxDeclarativeUI::reLayout()
{
    QLayout *layout = gb->parentWidget()->layout();
    if (layout) {
        layout->activate();
        layout->update();
    }
}

void QGroupBoxDeclarativeUI::finish()
{
    gb->setAnimated(false);
    if (m_expanded) {
        showChildren();
1026
        gb->setMinimumHeight(m_oldMinHeight);
1027
        gb->setMaximumHeight(m_oldMAxHeight);
1028
1029
        gb->resize(gb->sizeHint());
        gb->finishFirstExpand();
1030
1031
    }
    else {
1032
        gb->setMinimumHeight(30);
1033
        gb->setMaximumHeight(30);
1034
        gb->resize(gb->sizeHint().width(), 30);
1035
1036
1037
1038
1039
1040
1041
1042
1043
    }
    reLayout();
}

void QGroupBoxDeclarativeUI::hideChildren()
{
    if (gb->isVisible()) {
        gb->setMinimumHeight(gb->height());
        foreach (QWidget *widget, gb->findChildren<QWidget*>())
1044
1045
            if (widget->parent() == gb) {
                widget->setProperty("wasVisibleGB", widget->property("visible"));
1046
                widget->hide();
1047
            }
1048
1049
1050
1051
1052
1053
    }
}

void QGroupBoxDeclarativeUI::showChildren()
{
    foreach (QWidget *widget, gb->findChildren<QWidget*>()) {
1054
1055
1056
1057
1058
1059
        if (widget->parent() == gb) {
            if (widget->property("wasVisibleGB").toBool())
                widget->show();
            widget->setProperty("wasVisibleGB", QVariant());
            widget->ensurePolished();
        }
1060
1061
1062
1063
1064
1065
1066
1067
    }
    gb->show();
}

void QGroupBoxDeclarativeUI::collapse()
{
    m_oldMAxHeight = gb->maximumHeight();
    m_oldHeight = gb->height();
1068
    m_oldMinHeight =gb->minimumHeight();
1069
1070
1071
1072
    if (!m_expanded)
        return;
    m_contens = QPixmap::grabWidget (gb, 5, 5, gb->width() - 5, gb->height() - 5);
    gb->setPixmap(m_contens,1);
1073
    hideChildren();
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
    m_expanded = false;
    m_timeLine.start();
}

void QGroupBoxDeclarativeUI::expand()
{
    if (m_expanded)
        return;
    m_expanded = true;
    m_timeLine.start();
}

void QGroupBoxDeclarativeUI::animate(int frame)
{
    gb->setAnimated(true);
    qreal height;

    if (m_expanded) {
        height = ((qreal(frame) / 5.0) * qreal(m_oldHeight)) + (30.0 * (1.0 - qreal(frame) / 5.0));
        gb->setPixmap(m_contens, qreal(frame) / 5.0);
    }
    else {
        height = ((qreal(frame) / 5.0) * 30.0) + (qreal(m_oldHeight) * (1.0 - qreal(frame) / 5.0));
        qreal alpha = 0.8 - qreal(frame) / 4.0;
        if (alpha < 0)
            alpha = 0;
        gb->setPixmap(m_contens, alpha);
    }

    gb->setMaximumHeight(height);
    gb->setMinimumHeight(height);
    reLayout();
    gb->update();
}

class QTabWidgetDeclarativeUI : public QObject
{
    Q_OBJECT

1113
    Q_PROPERTY(QmlListProperty<QTabObject> tabs READ tabs)
1114
1115
    Q_CLASSINFO("DefaultProperty", "tabs")
public:
1116
    QTabWidgetDeclarativeUI(QObject *other) : QObject(other) {}
1117

1118
1119
1120
    QmlListProperty<QTabObject> tabs() {
        return QmlListProperty<QTabObject>(this, 0, tabs_append, 0, 0, tabs_clear);
    }
1121
1122

private:
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
    static void tabs_append(QmlListProperty<QTabObject> *property, QTabObject *o) {
        QTabWidget *tw = static_cast<QTabWidget*>(property->object->parent());
        if (!o->icon().isNull())
            tw->addTab(o->content(), o->icon(), o->label());
        else
            tw->addTab(o->content(), o->label());
    }

    static void tabs_clear(QmlListProperty<QTabObject> *property) {
        QTabWidget *tw = static_cast<QTabWidget*>(property->object->parent());
        tw->clear();
    }
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
};


class WidgetFrame : public QFrame
{
    Q_OBJECT
public:
    WidgetFrame( QWidget * parent = 0, Qt::WindowFlags f = 0 ) : QFrame(parent, f)
    {}
};

1146
QML_DEFINE_EXTENDED_TYPE(Bauhaus,1,0,QWidget,QWidget,QWidgetDeclarativeUI);
1147
1148

//display
1149
1150
QML_DEFINE_TYPE(Bauhaus,1,0,QProgressBar,QProgressBar)
QML_DEFINE_TYPE(Bauhaus,1,0,QLCDNumber,QLCDNumber)
1151
1152

//input
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
QML_DEFINE_TYPE(Bauhaus,1,0,QLineEdit,QLineEdit)
QML_DEFINE_TYPE(Bauhaus,1,0,QTextEdit,QTextEdit)
QML_DEFINE_TYPE(Bauhaus,1,0,QPlainTextEdit,QPlainTextEdit)
QML_DEFINE_TYPE(Bauhaus,1,0,QSpinBox,QSpinBox)
QML_DEFINE_TYPE(Bauhaus,1,0,QDoubleSpinBox,QDoubleSpinBox)
QML_DEFINE_TYPE(Bauhaus,1,0,QSlider,QSlider)
QML_DEFINE_TYPE(Bauhaus,1,0,QDateTimeEdit,QDateTimeEdit)
QML_DEFINE_TYPE(Bauhaus,1,0,QDateEdit,QDateEdit)
QML_DEFINE_TYPE(Bauhaus,1,0,QTimeEdit,QTimeEdit)
QML_DEFINE_TYPE(Bauhaus,1,0,QFontComboBox,QFontComboBox)
QML_DEFINE_TYPE(Bauhaus,1,0,QDial,QDial)
QML_DEFINE_TYPE(Bauhaus,1,0,QScrollBar,QScrollBar)
QML_DEFINE_TYPE(Bauhaus,1,0,QCalendarWidget, QCalendarWidget)


QML_DECLARE_TYPE(MyGroupBox)
QML_DECLARE_TYPE(WidgetLoader)
QML_DECLARE_TYPE(WidgetFrame)
//QML_DEFINE_TYPE(Bauhaus,1,0,QComboBox,QComboBox); //need a way to populate
1172
1173
1174
//QML_DEFINE_EXTENDED_TYPE(QComboBox,QComboBox, QComboBox); //need a way to populate

//buttons
1175
1176
//QML_DEFINE_TYPE(Bauhaus,1,0,QPushButton,QPushButton);
QML_DEFINE_TYPE(Bauhaus,1,0,QCheckBox,QCheckBox)
1177
QML_DEFINE_TYPE(Bauhaus,1,0,QGroupBox,QGroupBox)
1178
1179
1180
1181
QML_DEFINE_TYPE(Bauhaus,1,0,QAction,Action)
QML_DEFINE_TYPE(Bauhaus,1,0,QRadioButton,QRadioButton)
QML_DEFINE_TYPE(Bauhaus,1,0,FileWidget, FileWidget)
QML_DEFINE_TYPE(Bauhaus,1,0,LayoutWidget, LayoutWidget)
1182
1183

//containers
1184
1185
1186
1187
1188
1189
1190
QML_DEFINE_TYPE(Bauhaus,1,0,QFrame,QFrame)
QML_DEFINE_TYPE(Bauhaus,1,0,WidgetFrame,WidgetFrame)
QML_DEFINE_TYPE(Bauhaus,1,0,WidgetLoader,WidgetLoader)
QML_DEFINE_EXTENDED_TYPE(Bauhaus,1,0,QExtGroupBox,MyGroupBox,QGroupBoxDeclarativeUI)
QML_DEFINE_EXTENDED_TYPE(Bauhaus,1,0,QTabWidget,QTabWidget,QTabWidgetDeclarativeUI)
QML_DEFINE_EXTENDED_TYPE(Bauhaus,1,0,QScrollArea,QScrollArea,QScrollAreaDeclarativeUI)
QML_DEFINE_EXTENDED_TYPE(Bauhaus,1,0,QPushButton,QPushButton,QPushButtonDeclarativeUI)
1191
QML_DEFINE_EXTENDED_TYPE(Bauhaus,1,0,QLabel,QLabel,QLabelDeclarativeUI)
1192
1193
1194
1195
1196
1197
1198
QML_DEFINE_EXTENDED_TYPE(Bauhaus,1,0,QToolButton,QToolButton, QToolButtonDeclarativeUI)
QML_DEFINE_EXTENDED_TYPE(Bauhaus,1,0,QComboBox,QComboBox, QComboBoxDeclarativeUI)
QML_DEFINE_EXTENDED_TYPE(Bauhaus,1,0,QMenu,QMenu, QMenuDeclarativeUI)
//QML_DEFINE_TYPE(Bauhaus,1,0,BauhausoolBox,BauhausoolBox);
//QML_DEFINE_TYPE(Bauhaus,1,0,QScrollArea,QScrollArea);

//QML_DEFINE_EXTENDED_TYPE(BauhausColorButton,BauhausColorButton,BauhausColorButtonDeclarativeUI);
1199
1200

//itemviews
1201
1202
1203
//QML_DEFINE_TYPE(Bauhaus,1,0,QListView,QListView);
//QML_DEFINE_TYPE(Bauhaus,1,0,BauhausreeView,BauhausreeView);
//QML_DEFINE_TYPE(Bauhaus,1,0,BauhausableView,BauhausableView);
1204

1205
1206
QML_DECLARE_TYPE(QTabObject);
QML_DEFINE_TYPE(Qt,4,6,QTabObject,QTabObject); //### with namespacing, this should just be 'Tab'
1207

hjk's avatar
hjk committed
1208
1209
QT_END_NAMESPACE

1210
#include "basicwidgets.moc"