dtkWidgetsParameterFileSave.cpp 2.04 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// Version: $Id$
//
//

// Commentary:
//
//

// Change Log:
//
//

// Code:

#include "dtkWidgetsParameterFileSave.h"

17
#include <QtWidgets>
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

// ///////////////////////////////////////////////////////////////////
// dtkWidgetsParameterFileSavePrivate declaration
// ///////////////////////////////////////////////////////////////////

class dtkWidgetsParameterFileSavePrivate
{
public:
    QPushButton *push_button = nullptr;
};

// ///////////////////////////////////////////////////////////////////
// dtkWidgetsParameterFileSave implementation
// ///////////////////////////////////////////////////////////////////

dtkWidgetsParameterFileSave::dtkWidgetsParameterFileSave(QWidget* parent) : dtkWidgetsParameterBase<dtk::d_path>(parent), d(new dtkWidgetsParameterFileSavePrivate)
{
    d->push_button = new QPushButton;

    QHBoxLayout *layout = new QHBoxLayout;
    layout->addWidget(d->push_button);

    this->setLayout(layout);
}

dtkWidgetsParameterFileSave::~dtkWidgetsParameterFileSave(void)
{
    delete d;
}

bool dtkWidgetsParameterFileSave::connect(dtkCoreParameter *p)
{
    if (!p) {
        qWarning() << Q_FUNC_INFO << "The input parameter is null. Nothing is done.";
        return false;
    }

    m_parameter = dynamic_cast<dtk::d_path *>(p);

    if(!m_parameter) {
        qWarning() << Q_FUNC_INFO << "The type of the parameter is not compatible with the widget dtkWidgetsParameterFileSave.";
        return false;
    }

    d->push_button->setText("Save");

    m_parameter->connect([=] (QVariant v) {
        d->push_button->blockSignals(true);
        QString value = v.value<dtk::d_path>().baseName();
        if (value.isEmpty()) {
            value = "Save";
        }
        d->push_button->setText(value);
        d->push_button->blockSignals(false);
    });

    QObject::connect(d->push_button, &QPushButton::clicked, [=] ()
    {
76
        m_parameter->setValue(QFileDialog::getSaveFileName(this, "Save file", m_parameter->dirName(), m_parameter->filters().join(";;")));
77 78 79 80 81 82 83
    });

    return true;
}

//
// dtkWidgetsParameterFileSave.cpp ends here