dtkWidgetsLayout.cpp 2.94 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Version: $Id: c38ed7cb8f50e3cfa5bb6c151e0d9ce3c97e3ed4 $
//
//

// Commentary:
//
//

// Change Log:
//
//

// Code:

15
#include "dtkWidgetsController.h"
16 17 18
#include "dtkWidgetsLayout.h"
#include "dtkWidgetsLayoutItem.h"

KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
19 20 21
#include <QtCore>
#include <QtWidgets>

22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
/*!

  \class dtkWidgetsLayout

  \brief The dtkWidgetsLayout class is s composite class that acts as a
  visual proxy to the layout item interface. It actually composes a root
  node.

  \since 2.0.0

  \ingroup dtkWidgets

  dtkWidgetsLayout is a top level frame that instanciates a root node,
  which actally is a binary tree. It is responsible for satisfying the
  node's interface, when it comes to its ui.

  \sa dtkWidgetsLayoutItem, dtkWidgetsItemProxy

*/

42 43 44
class dtkWidgetsLayoutPrivate
{
public:
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
45 46
    dtkWidgetsLayoutItem *root = nullptr;
    dtkWidgetsLayoutItem *current = nullptr;
47 48 49

public:
    dtkWidgetsLayout::creator creator = nullptr;
50 51
};

52 53 54 55 56 57 58 59 60 61 62 63 64 65
/*!

  \fn dtkWidgetsLayout::dtkWidgetsLayout(QWidget *parent)

  \brief Construcs a dtkWidgetsLayout

  \ingroup dtkWidgets

  Inherits QFrame to be styled.

  \sa QFrame

*/

66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
dtkWidgetsLayout::dtkWidgetsLayout(QWidget *parent) : QFrame(parent), d(new dtkWidgetsLayoutPrivate)
{
    d->root = new dtkWidgetsLayoutItem(0);
    d->root->setParent(this);
    d->root->setLayout(this);

    d->current = d->root;

    QVBoxLayout *layout = new QVBoxLayout(this);
    layout->setContentsMargins(0, 0, 0, 0);
    layout->setSpacing(0);
    layout->addWidget(d->root);

    connect(d->root, SIGNAL(focused(dtkWidgetsWidget *)), this, SIGNAL(focused(dtkWidgetsWidget *)));
    connect(d->root, SIGNAL(unfocused(dtkWidgetsWidget *)), this, SIGNAL(unfocused(dtkWidgetsWidget *)));
81 82
    // TODO: // connect(d->root, SIGNAL(create()), this, SIGNAL(create()));
    connect(d->root, SIGNAL(create(const QString&)), this, SLOT(create(const QString&)));
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
}

dtkWidgetsLayout::~dtkWidgetsLayout(void)
{
    delete d->root;

    delete d;

    d = NULL;
}

dtkWidgetsLayoutItem *dtkWidgetsLayout::root(void)
{
    return d->root;
}

dtkWidgetsLayoutItem *dtkWidgetsLayout::current(void)
{
    return d->current;
}

104 105 106 107 108
void dtkWidgetsLayout::setCreator(dtkWidgetsLayout::creator creator)
{
    d->creator = creator;
}

109 110 111 112 113 114
void dtkWidgetsLayout::setCurrent(dtkWidgetsLayoutItem *item)
{
    disconnect(d->current, 0, 0, 0);

    d->current = item;

WINTZ Julien's avatar
WINTZ Julien committed
115
    connect(d->current, SIGNAL(create()), this, SLOT(create()));
116
    connect(d->current, SIGNAL(create(const QString&)), this, SLOT(create(const QString&)));
117 118 119 120 121 122 123
}

void dtkWidgetsLayout::clear(void)
{
    d->root->clear();
}

WINTZ Julien's avatar
WINTZ Julien committed
124 125 126 127 128
void dtkWidgetsLayout::create(void)
{
    this->create("");
}

129 130 131 132 133 134 135
void dtkWidgetsLayout::create(const QString& name)
{
    if(!d->creator)
        return;

    dtkWidgetsWidget *widget = d->creator(name);

136 137
    d->current->setView(widget);

138 139 140 141
    dtkWidgetsController::instance()->insert(widget);

}

142 143 144 145 146 147 148 149 150
void dtkWidgetsLayout::keyPressEvent(QKeyEvent *event)
{
    event->ignore();

    QFrame::keyPressEvent(event);
}

//
// dtkWidgetsLayout.cpp ends here