main.cpp 8.6 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 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
// Version: $Id$
//
//

// Commentary:
//
//

// Change Log:
//
//

// Code:

#include <QtCore>
#include <QtWidgets>

#include <dtkFonts>
#include <dtkWidgets>

// ///////////////////////////////////////////////////////////////////
//
// ///////////////////////////////////////////////////////////////////

class dtkWidgetsScrollerHUDItem : public QLabel
{
    Q_OBJECT

public:
    dtkWidgetsScrollerHUDItem(int id, QWidget *parent = nullptr) : QLabel(parent) {

        dtkFontAwesome::instance()->initFontAwesome();
        dtkFontAwesome::instance()->setDefaultOption("scale-factor", 1.0);
        dtkFontAwesome::instance()->setDefaultOption("color", QColor("#dddddd"));

        this->id = id;

        this->setPixmap(dtkFontAwesome::instance()->icon(fa::circlethin).pixmap(8, 8));
        this->setFixedWidth(8);
        this->setFixedHeight(8);

        if(!id)
            this->toggle(true);

        this->setMouseTracking(true);
    }

signals:
    void clicked(void);

public slots:
    void toggle(bool on) {
        this->setPixmap(dtkFontAwesome::instance()->icon(on ? fa::circle : fa::circlethin).pixmap(8, 8));
    }

protected:
    void mousePressEvent(QMouseEvent *) override {
        emit clicked();
    }

public:
    int id = -1;
};

// ///////////////////////////////////////////////////////////////////
//
// ///////////////////////////////////////////////////////////////////

class dtkWidgetsScrollerHUD : public QFrame
{
    Q_OBJECT

public:
     dtkWidgetsScrollerHUD(QWidget *parent = nullptr);
    ~dtkWidgetsScrollerHUD(void);

signals:
    void slide(int);

public:
    void increase(void);
    void decrease(void);
};

dtkWidgetsScrollerHUD::dtkWidgetsScrollerHUD(QWidget *parent) : QFrame(parent)
{
    QVBoxLayout *layout = new QVBoxLayout(this);
    layout->setContentsMargins(0, 0, 0, 0);
    layout->setAlignment(Qt::AlignVCenter);

    this->setAttribute(Qt::WA_NoSystemBackground);
    this->setAttribute(Qt::WA_TranslucentBackground);
}

dtkWidgetsScrollerHUD::~dtkWidgetsScrollerHUD(void)
{

}

void dtkWidgetsScrollerHUD::increase(void)
{
    static int count = 0;

    dtkWidgetsScrollerHUDItem *item = new dtkWidgetsScrollerHUDItem(count++, this);

    connect(item, &dtkWidgetsScrollerHUDItem::clicked, [=] () {

        for(int i = 0; i < this->layout()->count(); i++)
            if(dtkWidgetsScrollerHUDItem *hud_item = dynamic_cast<dtkWidgetsScrollerHUDItem *>(this->layout()->itemAt(i)->widget()))
                hud_item->toggle(hud_item->id == item->id);

        emit slide(item->id);
    });

    this->layout()->addWidget(item);
}

void dtkWidgetsScrollerHUD::decrease(void)
{
    // TODO
}

// ///////////////////////////////////////////////////////////////////
//
// ///////////////////////////////////////////////////////////////////

WINTZ Julien's avatar
WINTZ Julien committed
127 128 129 130 131
class dtkWidgetsScrollerPage : public QFrame
{
    Q_OBJECT

public:
WINTZ Julien's avatar
WINTZ Julien committed
132 133 134 135
    dtkWidgetsScrollerPage(const QString& text, QWidget *parent = nullptr);

public:
    QLabel *label;
WINTZ Julien's avatar
WINTZ Julien committed
136 137
};

WINTZ Julien's avatar
WINTZ Julien committed
138
dtkWidgetsScrollerPage::dtkWidgetsScrollerPage(const QString& text, QWidget *parent) : QFrame(parent)
WINTZ Julien's avatar
WINTZ Julien committed
139
{
WINTZ Julien's avatar
WINTZ Julien committed
140 141 142 143 144 145 146 147
    this->label = new QLabel(text, this);
    this->label->setAlignment(Qt::AlignCenter);

    QVBoxLayout *layout = new QVBoxLayout(this);
    layout->setAlignment(Qt::AlignCenter);
    layout->setContentsMargins(0, 0, 0, 0);
    layout->addWidget(this->label);

WINTZ Julien's avatar
WINTZ Julien committed
148 149 150 151 152 153 154
    this->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
}

// ///////////////////////////////////////////////////////////////////
//
// ///////////////////////////////////////////////////////////////////

WINTZ Julien's avatar
WINTZ Julien committed
155 156 157 158 159 160 161
class dtkWidgetsScroller : public QScrollArea
{
    Q_OBJECT

public:
    dtkWidgetsScroller(QWidget *parent = nullptr);

WINTZ Julien's avatar
WINTZ Julien committed
162 163 164 165
public:
    void    addPage(dtkWidgetsScrollerPage *);
    void removePage(dtkWidgetsScrollerPage *);

166 167 168 169 170 171
public slots:
    void scrollTo(int);

protected:
    void scroll(int dx);

WINTZ Julien's avatar
WINTZ Julien committed
172
protected:
173
    void resizeEvent(QResizeEvent *) override;
WINTZ Julien's avatar
WINTZ Julien committed
174 175
    void wheelEvent(QWheelEvent *) override;

WINTZ Julien's avatar
WINTZ Julien committed
176 177
public:
    QVBoxLayout *layout;
178 179 180

public:
    int from = 0;
WINTZ Julien's avatar
WINTZ Julien committed
181 182 183 184 185 186 187 188 189 190 191 192
};

dtkWidgetsScroller::dtkWidgetsScroller(QWidget *parent) : QScrollArea(parent)
{
    this->layout = new QVBoxLayout;
    this->layout->setContentsMargins(0, 0, 0, 0);
    this->layout->setSpacing(0);

    QWidget *contents = new QWidget(this);
    contents->setLayout(this->layout);

    this->setFrameShape(QFrame::NoFrame);
WINTZ Julien's avatar
WINTZ Julien committed
193
    this->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
WINTZ Julien's avatar
WINTZ Julien committed
194 195 196 197
    this->setWidgetResizable(true);
    this->setWidget(contents);
}

WINTZ Julien's avatar
WINTZ Julien committed
198 199 200 201 202 203 204 205 206 207
void dtkWidgetsScroller::addPage(dtkWidgetsScrollerPage *page)
{
    this->layout->addWidget(page);
}

void dtkWidgetsScroller::removePage(dtkWidgetsScrollerPage *page)
{
    this->layout->removeWidget(page);
}

208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
void dtkWidgetsScroller::scrollTo(int to)
{
    if(this->layout->count() <= 1)
        return;

    if(to > this->layout->count())
        return;

    if(to < 0)
        return;

    double xf = this->from; this->from = to;
    double xt = to;

    double ss = this->verticalScrollBar()->value();
    double sm = this->verticalScrollBar()->maximum();
    double sc = this->layout->count() - 1;

    QVariantAnimation *animation = new QVariantAnimation;
    animation->setEasingCurve(QEasingCurve::OutCubic);
    animation->setStartValue(xf);
    animation->setEndValue(xt);
    animation->setDuration(500);

    connect(animation, &QVariantAnimation::valueChanged, [=] (const QVariant& value) {
        this->scroll(ss + (value.toDouble() - xf) * sm / sc);
    });

    animation->start(QAbstractAnimation::DeleteWhenStopped);
}

void dtkWidgetsScroller::scroll(int dx)
{
    this->verticalScrollBar()->setValue(dx);
}

244 245 246 247 248 249
void dtkWidgetsScroller::resizeEvent(QResizeEvent *event)
{
    for(int i = 0; i < this->layout->count(); i++)
        if(dtkWidgetsScrollerPage *page = dynamic_cast<dtkWidgetsScrollerPage *>(layout->itemAt(i)->widget()))
            page->setFixedHeight(event->size().height());

250 251 252
    this->repaint();
    this->scrollTo(this->from);

253 254 255
    QScrollArea::resizeEvent(event);
}

WINTZ Julien's avatar
WINTZ Julien committed
256 257
void dtkWidgetsScroller::wheelEvent(QWheelEvent *event)
{
258 259 260 261 262 263
    // TODO: Need to handle kinetic scrolling.

    // if (event->angleDelta().y() > 0)
    //     this->scrollTo(this->from-1);
    // else
    //     this->scrollTo(this->from+1);
WINTZ Julien's avatar
WINTZ Julien committed
264 265
}

WINTZ Julien's avatar
WINTZ Julien committed
266 267 268 269
// ///////////////////////////////////////////////////////////////////
//
// ///////////////////////////////////////////////////////////////////

270 271 272 273 274 275 276 277 278 279 280 281
class dummy : public QFrame
{
    Q_OBJECT

public:
    dummy(QWidget *parent = nullptr);

protected:
    void resizeEvent(QResizeEvent *);

private:
    dtkWidgetsScrollerHUD *hud;
WINTZ Julien's avatar
WINTZ Julien committed
282
    dtkWidgetsScroller *scroll;
283 284 285 286
};

dummy::dummy(QWidget *parent) : QFrame(parent)
{
WINTZ Julien's avatar
WINTZ Julien committed
287
    dtkWidgetsScrollerPage *page1 = new dtkWidgetsScrollerPage("Page 1", this);
WINTZ Julien's avatar
WINTZ Julien committed
288
    page1->setFixedHeight(480);
WINTZ Julien's avatar
WINTZ Julien committed
289
    page1->setStyleSheet("QFrame { background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #26aae5, stop: 1.0 #1e5fa7); } QLabel { background: none; }");
WINTZ Julien's avatar
WINTZ Julien committed
290

WINTZ Julien's avatar
WINTZ Julien committed
291
    dtkWidgetsScrollerPage *page2 = new dtkWidgetsScrollerPage("Page 2", this);
WINTZ Julien's avatar
WINTZ Julien committed
292
    page2->setFixedHeight(480);
WINTZ Julien's avatar
WINTZ Julien committed
293
    page2->setStyleSheet("QFrame { background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #4c37a0, stop: 1.0 #182a58); } QLabel { background: none; }");
WINTZ Julien's avatar
WINTZ Julien committed
294

WINTZ Julien's avatar
WINTZ Julien committed
295
    dtkWidgetsScrollerPage *page3 = new dtkWidgetsScrollerPage("Page 3", this);
WINTZ Julien's avatar
WINTZ Julien committed
296
    page3->setFixedHeight(480);
WINTZ Julien's avatar
WINTZ Julien committed
297
    page3->setStyleSheet("QFrame { background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #fe50b7, stop: 1.0 #f03a58); } QLabel { background: none; }");
WINTZ Julien's avatar
WINTZ Julien committed
298

WINTZ Julien's avatar
WINTZ Julien committed
299
    this->scroll = new dtkWidgetsScroller(this);
WINTZ Julien's avatar
WINTZ Julien committed
300 301 302 303
    this->scroll->setAlignment(Qt::AlignTop);
    this->scroll->addPage(page1);
    this->scroll->addPage(page2);
    this->scroll->addPage(page3);
WINTZ Julien's avatar
WINTZ Julien committed
304

305 306 307 308
    this->hud = new dtkWidgetsScrollerHUD(this);
    this->hud->increase();
    this->hud->increase();
    this->hud->increase();
WINTZ Julien's avatar
WINTZ Julien committed
309 310 311 312 313 314

    QVBoxLayout *layout = new QVBoxLayout(this);
    layout->setContentsMargins(0, 0, 0, 0);
    layout->addWidget(this->scroll);

    connect(this->hud, &dtkWidgetsScrollerHUD::slide, [=] (int index) {
315
        this->scroll->scrollTo(index);
WINTZ Julien's avatar
WINTZ Julien committed
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 350
}

void dummy::resizeEvent(QResizeEvent *event)
{
    static int size = 8;
    static int margin = 4;

    this->hud->move(event->size().width() - size - margin, 0);
    this->hud->resize(size, event->size().height());
}

// ///////////////////////////////////////////////////////////////////
//
// ///////////////////////////////////////////////////////////////////

int main(int argc, char **argv)
{
    QApplication application(argc, argv);

    dummy *dummy = new class dummy;
    dummy->setAttribute(Qt::WA_DeleteOnClose, true);
    dummy->resize(800, 480);
    dummy->show();
    dummy->raise();

    return application.exec();
}

// ///////////////////////////////////////////////////////////////////

#include "main.moc"

//
// main.cpp ends here