krita/ui

kis_channelflags_widget.cpp

Go to the documentation of this file.
00001 /*
00002  *  Copyright (c) 2007 Boudewijn Rempt <boud@valdyas.org>
00003  *
00004  *  This program is free software; you can redistribute it and/or modify
00005  *  it under the terms of the GNU General Public License as published by
00006  *  the Free Software Foundation; either version 2 of the License, or
00007  *  (at your option) any later version.
00008  *
00009  *  This program is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *  GNU General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU General Public License
00015  *  along with this program; if not, write to the Free Software
00016  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017  */
00018 
00019 #include "widgets/kis_channelflags_widget.h"
00020 
00021 #include <QBitArray>
00022 #include <QScrollArea>
00023 #include <QWidget>
00024 #include <QCheckBox>
00025 #include <QVBoxLayout>
00026 
00027 #include <klocale.h>
00028 
00029 #include <KoChannelInfo.h>
00030 #include <KoColorSpace.h>
00031 
00032 #include <kis_debug.h>
00033 
00034 KisChannelFlagsWidget::KisChannelFlagsWidget(const KoColorSpace * colorSpace, QWidget * parent)
00035         : QScrollArea(parent)
00036         , m_colorSpace(colorSpace)
00037 {
00038     setObjectName("KisChannelFlagsWidget");
00039     setToolTip(i18n("Check the active channels in this layer. Only these channels will be affected by any operation."));
00040     QWidget * w = new QWidget();
00041     setBackgroundRole(QPalette::Window);
00042     QVBoxLayout * vbl = new QVBoxLayout();
00043 
00044     for (int i = 0; i < colorSpace->channels().size(); ++i) {
00045         KoChannelInfo * channel = colorSpace->channels().at(i);
00046         QCheckBox * bx = new QCheckBox(channel->name(), w);
00047         bx->setCheckState(Qt::Checked);
00048         vbl->addWidget(bx);
00049         m_channelChecks.append(bx);
00050     }
00051 
00052     w->setLayout(vbl);
00053     setWidget(w);
00054 
00055 }
00056 
00057 KisChannelFlagsWidget::~KisChannelFlagsWidget()
00058 {
00059 }
00060 
00061 void KisChannelFlagsWidget::setChannelFlags(const QBitArray & cf)
00062 {
00063     dbgUI << "KisChannelFlagsWidget::setChannelFlags " << cf.isEmpty();
00064     if (cf.isEmpty()) return;
00065 
00066     QBitArray channelFlags = m_colorSpace->setChannelFlagsToColorSpaceOrder(cf);
00067     for (int i = 0; i < qMin(m_channelChecks.size(), channelFlags.size()); ++i) {
00068         m_channelChecks.at(i)->setChecked(channelFlags.testBit(i));
00069     }
00070 }
00071 
00072 QBitArray KisChannelFlagsWidget::channelFlags() const
00073 {
00074     bool allTrue = true;
00075     QBitArray ba(m_channelChecks.size());
00076 
00077     for (int i = 0; i < m_channelChecks.size(); ++i) {
00078 
00079         bool flag = m_channelChecks.at(i)->isChecked();
00080         if (!flag) allTrue = false;
00081 
00082         ba.setBit(i, flag);
00083         dbgUI << " channel " << i << " is " << flag << ", allTrue = " << allTrue << ", so ba.testBit(" << i << ")" << " is " << ba.testBit(i);
00084     }
00085     if (allTrue)
00086         return QBitArray();
00087     else {
00088         QBitArray result = m_colorSpace->setChannelFlagsToPixelOrder(ba);
00089         for (int i = 0; i < result.size(); ++i) {
00090             dbgUI << "On conversion to the pixel order, flag " << i << " is " << result.testBit(i);
00091         }
00092         return result;
00093     }
00094 }