kis_color_selector_container.cpp 9.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 *  Copyright (c) 2010 Adam Celarek <kdedev at xibo dot at>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 */

Adam Celarek's avatar
Adam Celarek committed
18 19 20 21 22 23 24 25 26
#include "kis_color_selector_container.h"

#include "kis_color_selector.h"
#include "kis_my_paint_shade_selector.h"
#include "kis_minimal_shade_selector.h"

#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QPushButton>
Boudewijn Rempt's avatar
Boudewijn Rempt committed
27
#include <QAction>
Adam Celarek's avatar
Adam Celarek committed
28

29 30
#include <kconfig.h>
#include <kconfiggroup.h>
Boudewijn Rempt's avatar
Boudewijn Rempt committed
31
#include <ksharedconfig.h>
Boudewijn Rempt's avatar
Boudewijn Rempt committed
32

33
#include <kactioncollection.h>
34

35
#include <KisDocument.h>
36
#include <KisGamutMaskToolbar.h>
37
#include "KisViewManager.h"
38 39
#include "kis_canvas2.h"
#include "kis_canvas_resource_provider.h"
40
#include "kis_node_manager.h"
Boudewijn Rempt's avatar
Boudewijn Rempt committed
41 42
#include "kis_node.h"
#include "kis_paint_device.h"
43

Michael Abrahams's avatar
Michael Abrahams committed
44 45
#include "kis_action_registry.h"

Adam Celarek's avatar
Adam Celarek committed
46 47 48 49 50 51
KisColorSelectorContainer::KisColorSelectorContainer(QWidget *parent) :
    QWidget(parent),
    m_colorSelector(new KisColorSelector(this)),
    m_myPaintShadeSelector(new KisMyPaintShadeSelector(this)),
    m_minimalShadeSelector(new KisMinimalShadeSelector(this)),
    m_shadeSelector(m_myPaintShadeSelector),
52
    m_gamutMaskToolbar(new KisGamutMaskToolbar(this)),
53
    m_showColorSelector(true),
54
    m_canvas(0)
Adam Celarek's avatar
Adam Celarek committed
55
{
56 57 58 59
    m_widgetLayout = new QBoxLayout(QBoxLayout::TopToBottom, this);
    m_widgetLayout->setSpacing(0);
    m_widgetLayout->setMargin(0);

60 61 62 63 64 65 66
    m_gamutMaskToolbar->setContentsMargins(0, 0, 0, 5);
    m_gamutMaskToolbar->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum);
    m_colorSelector->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    m_myPaintShadeSelector->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    m_minimalShadeSelector->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

    m_widgetLayout->addWidget(m_gamutMaskToolbar);
67 68 69
    m_widgetLayout->addWidget(m_colorSelector);
    m_widgetLayout->addWidget(m_myPaintShadeSelector);
    m_widgetLayout->addWidget(m_minimalShadeSelector);
Adam Celarek's avatar
Adam Celarek committed
70

71
    m_gamutMaskToolbar->hide();
Adam Celarek's avatar
Adam Celarek committed
72 73
    m_myPaintShadeSelector->hide();
    m_minimalShadeSelector->hide();
74

75 76
    connect(m_colorSelector,SIGNAL(settingsButtonClicked()), SIGNAL(openSettings()));

77 78 79 80
    connect(this, SIGNAL(settingsChanged()), m_colorSelector,        SLOT(updateSettings()));
    connect(this, SIGNAL(settingsChanged()), m_myPaintShadeSelector, SLOT(updateSettings()));
    connect(this, SIGNAL(settingsChanged()), this,                   SLOT(updateSettings()));
    connect(this, SIGNAL(settingsChanged()), m_minimalShadeSelector, SLOT(updateSettings()));
81

82

Michael Abrahams's avatar
Michael Abrahams committed
83
    m_colorSelAction = KisActionRegistry::instance()->makeQAction("show_color_selector", this);
84 85
    connect(m_colorSelAction, SIGNAL(triggered()), m_colorSelector, SLOT(showPopup()), Qt::UniqueConnection);

Michael Abrahams's avatar
Michael Abrahams committed
86
    m_mypaintAction = KisActionRegistry::instance()->makeQAction("show_mypaint_shade_selector", this);
87 88
    connect(m_mypaintAction, SIGNAL(triggered()), m_myPaintShadeSelector, SLOT(showPopup()), Qt::UniqueConnection);

Michael Abrahams's avatar
Michael Abrahams committed
89
    m_minimalAction = KisActionRegistry::instance()->makeQAction("show_minimal_shade_selector", this);
90 91
    connect(m_minimalAction, SIGNAL(triggered()), m_minimalShadeSelector, SLOT(showPopup()), Qt::UniqueConnection);

Adam Celarek's avatar
Adam Celarek committed
92 93
}

94 95
void KisColorSelectorContainer::unsetCanvas()
{
96 97
    m_colorSelector->hasAtLeastOneDocument(doesAtleastOneDocumentExist());

98 99 100 101 102 103
    m_colorSelector->unsetCanvas();
    m_myPaintShadeSelector->unsetCanvas();
    m_minimalShadeSelector->unsetCanvas();
    m_canvas = 0;
}

104 105 106 107 108 109 110 111 112 113 114 115 116
bool KisColorSelectorContainer::doesAtleastOneDocumentExist()
{
    if (m_canvas && m_canvas->viewManager() && m_canvas->viewManager()->document() ) {
        if (m_canvas->viewManager()->document()->image()->height() == 0) {
            return false;
        } else {
           return true;
        }
    } else {
        return false;
    }
}

117 118 119 120 121
void KisColorSelectorContainer::slotUpdateIcons()
{
    m_colorSelector->updateIcons();
}

122
void KisColorSelectorContainer::setCanvas(KisCanvas2* canvas)
Adam Celarek's avatar
Adam Celarek committed
123
{
124 125
    if (m_canvas) {
        m_canvas->disconnectCanvasObserver(this);
126 127
        m_canvas->viewManager()->nodeManager()->disconnect(this);
        KActionCollection *ac = m_canvas->viewManager()->actionCollection();
128 129 130 131 132 133 134
        ac->takeAction(ac->action("show_color_selector"));
        ac->takeAction(ac->action("show_mypaint_shade_selector"));
        ac->takeAction(ac->action("show_minimal_shade_selector"));
    }

    m_canvas = canvas;

135 136
    m_colorSelector->setCanvas(canvas);
    m_myPaintShadeSelector->setCanvas(canvas);
137
    m_minimalShadeSelector->setCanvas(canvas);
138

139 140 141 142

    m_colorSelector->hasAtLeastOneDocument(doesAtleastOneDocumentExist());


143 144 145 146
    if (m_canvas && m_canvas->viewManager()) {
        if (m_canvas->viewManager()->nodeManager()) {
            connect(m_canvas->viewManager()->nodeManager(), SIGNAL(sigLayerActivated(KisLayerSP)), SLOT(reactOnLayerChange()), Qt::UniqueConnection);
        }
147

148
        connect(m_canvas->viewManager()->canvasResourceProvider(), SIGNAL(sigGamutMaskChanged(KoGamutMask*)),
149
                m_colorSelector, SLOT(slotGamutMaskSet(KoGamutMask*)), Qt::UniqueConnection);
150

151
        connect(m_canvas->viewManager()->canvasResourceProvider(), SIGNAL(sigGamutMaskUnset()),
152
                m_colorSelector, SLOT(slotGamutMaskUnset()), Qt::UniqueConnection);
153

154
        connect(m_canvas->viewManager()->canvasResourceProvider(), SIGNAL(sigGamutMaskPreviewUpdate()),
155
                m_colorSelector, SLOT(slotGamutMaskPreviewUpdate()), Qt::UniqueConnection);
156

157 158
        connect(m_canvas->viewManager()->canvasResourceProvider(), SIGNAL(sigGamutMaskDeactivated()),
                m_colorSelector, SLOT(slotGamutMaskDeactivate()), Qt::UniqueConnection);
159

160
        m_gamutMaskToolbar->connectMaskSignals(m_canvas->viewManager()->canvasResourceProvider());
161

162 163 164 165
        KActionCollection* actionCollection = canvas->viewManager()->actionCollection();
        actionCollection->addAction("show_color_selector", m_colorSelAction);
        actionCollection->addAction("show_mypaint_shade_selector", m_mypaintAction);
        actionCollection->addAction("show_minimal_shade_selector", m_minimalAction);
166
    }
167 168
}

169 170
void KisColorSelectorContainer::updateSettings()
{
171
    KConfigGroup cfg =  KSharedConfig::openConfig()->group("advancedColorSelector");
172
    m_onDockerResizeSetting =  (int)cfg.readEntry("onDockerResize", 0);
173 174 175 176 177 178 179 180 181 182 183 184 185 186
    m_showColorSelector = (bool) cfg.readEntry("showColorSelector", true);

    if (m_showColorSelector) {
        m_colorSelector->show();

        if (m_colorSelector->configuration().mainType == KisColorSelectorConfiguration::Wheel) {
            m_gamutMaskToolbar->show();
        } else {
            m_gamutMaskToolbar->hide();
        }
    } else {
        m_colorSelector->hide();
        m_gamutMaskToolbar->hide();
    }
187

188
    QString type = cfg.readEntry("shadeSelectorType", "Minimal");
Adam Celarek's avatar
Adam Celarek committed
189

190 191 192 193 194 195 196 197
    QWidget* newShadeSelector;
    if(type=="MyPaint")
        newShadeSelector = m_myPaintShadeSelector;
    else if (type=="Minimal")
        newShadeSelector = m_minimalShadeSelector;
    else
        newShadeSelector = 0;

Boudewijn Rempt's avatar
Boudewijn Rempt committed
198

199

200 201
    if(m_shadeSelector!=newShadeSelector && m_shadeSelector!=0) {
        m_shadeSelector->hide();
Adam Celarek's avatar
Adam Celarek committed
202
    }
203
    m_shadeSelector=newShadeSelector;
Adam Celarek's avatar
Adam Celarek committed
204

205
    if(m_shadeSelector!=0)
Adam Celarek's avatar
Adam Celarek committed
206
        m_shadeSelector->show();
207 208
}

209 210
void KisColorSelectorContainer::reactOnLayerChange()
{
211
    if (m_canvas) {
212
        KisNodeSP node = m_canvas->viewManager()->canvasResourceProvider()->currentNode();
213 214 215 216 217 218 219 220 221 222 223 224
        if (node) {
            KisPaintDeviceSP device = node->paintDevice();
            if (device) {
                m_colorSelAction->setEnabled(true);
                m_mypaintAction->setEnabled(true);
                m_minimalAction->setEnabled(true);
            }
            else {
                //            m_colorSelAction->setEnabled(false);
                //            m_mypaintAction->setEnabled(false);
                //            m_minimalAction->setEnabled(false);
            }
225 226 227 228
        }
    }
}

Adam Celarek's avatar
Adam Celarek committed
229 230 231 232 233
void KisColorSelectorContainer::resizeEvent(QResizeEvent * e)
{
    if(m_shadeSelector!=0) {
        int minimumHeightForBothWidgets = m_colorSelector->minimumHeight()+m_shadeSelector->minimumHeight()+30; //+30 for the buttons (temporarily)

234
        if(height()<minimumHeightForBothWidgets && m_onDockerResizeSetting== 1) {  // 1 option is hide shade selector
235 236 237 238 239
            m_shadeSelector->hide();
        }
        else {
            m_shadeSelector->show();
        }
240 241 242

        // m_onDockerResizeSetting==0 is allow horizontal layout
        if(height() < width() && m_onDockerResizeSetting==0 && m_shadeSelector!=m_minimalShadeSelector) {
243
            m_widgetLayout->setDirection(QBoxLayout::LeftToRight);
Adam Celarek's avatar
Adam Celarek committed
244 245
        }
        else {
246
            m_widgetLayout->setDirection(QBoxLayout::TopToBottom);
Adam Celarek's avatar
Adam Celarek committed
247 248 249 250 251
        }
    }

    QWidget::resizeEvent(e);
}