File indexing completed on 2024-12-22 04:14:47

0001 /*
0002  *  SPDX-FileCopyrightText: 2011 Sven Langkamp <sven.langkamp@gmail.com>
0003  *
0004  *  SPDX-License-Identifier: LGPL-2.0-or-later
0005  */
0006 
0007 #include "channeldocker_dock.h"
0008 
0009 #include <QGridLayout>
0010 #include <QTableView>
0011 #include <QHeaderView>
0012 #include <klocalizedstring.h>
0013 
0014 #include <KoCanvasBase.h>
0015 #include "channelmodel.h"
0016 #include <KisViewManager.h>
0017 #include <kis_canvas2.h>
0018 #include <kis_layer.h>
0019 #include <kis_node_manager.h>
0020 #include <kis_image.h>
0021 #include <kis_group_layer.h>
0022 #include <kis_paint_device.h>
0023 #include <KisView.h>
0024 #include <kis_idle_watcher.h>
0025 #include "KisChannelsThumbnailsStrokeStrategy.h"
0026 #include <kis_display_color_converter.h>
0027 
0028 ChannelDockerDock::ChannelDockerDock()
0029 {
0030     setWindowTitle(i18nc("Channel as in Color Channels", "Channels"));
0031 
0032     m_channelTable = new QTableView(this);
0033     m_model = new ChannelModel(this);
0034     m_channelTable->setModel(m_model);
0035     m_channelTable->setShowGrid(false);
0036     m_channelTable->horizontalHeader()->setStretchLastSection(true);
0037     m_channelTable->verticalHeader()->setVisible(false);
0038     m_channelTable->horizontalHeader()->setVisible(false);
0039     m_channelTable->setSelectionBehavior( QAbstractItemView::SelectRows );
0040 
0041     QScroller *scroller = KisKineticScroller::createPreconfiguredScroller(m_channelTable);
0042     if (scroller){
0043         connect(scroller, SIGNAL(stateChanged(QScroller::State)),
0044                 this, SLOT(slotScrollerStateChanged(QScroller::State)));
0045     }
0046 
0047     setWidget(m_channelTable);
0048 
0049     connect(m_channelTable,&QTableView::activated, m_model, &ChannelModel::rowActivated);
0050 }
0051 
0052 void ChannelDockerDock::setCanvas(KoCanvasBase * canvas)
0053 {
0054     if (m_canvas) {
0055         m_canvas->disconnectCanvasObserver(this);
0056         m_canvas->image()->disconnect(this);
0057     }
0058 
0059     KisCanvas2 *newCanvas = canvas ? dynamic_cast<KisCanvas2*>(canvas) : nullptr;
0060 
0061     KisWidgetWithIdleTask<QDockWidget>::setCanvas(newCanvas);
0062     m_model->setCanvas(newCanvas);
0063 
0064     if (m_canvas) {
0065         connect(m_canvas->displayColorConverter(), SIGNAL(displayConfigurationChanged()), SLOT(startUpdateCanvasProjection()));
0066         connect(m_model, SIGNAL(channelFlagsChanged()), m_canvas, SLOT(channelSelectionChanged()));
0067     }
0068 
0069     setEnabled(bool(canvas));
0070 }
0071 
0072 void ChannelDockerDock::unsetCanvas()
0073 {
0074     setCanvas(0);
0075 }
0076 
0077 void ChannelDockerDock::startUpdateCanvasProjection()
0078 {
0079     triggerCacheUpdate();
0080 }
0081 
0082 KisIdleTasksManager::TaskGuard ChannelDockerDock::registerIdleTask(KisCanvas2 *canvas)
0083 {
0084     return
0085         canvas->viewManager()->idleTasksManager()->
0086         addIdleTaskWithGuard([this](KisImageSP image) {
0087             const KoColorProfile *profile =
0088                 m_canvas->displayColorConverter()->monitorProfile();
0089             KoColorConversionTransformation::ConversionFlags conversionFlags =
0090                 m_canvas->displayColorConverter()->conversionFlags();
0091             KoColorConversionTransformation::Intent renderingIntent =
0092                 m_canvas->displayColorConverter()->renderingIntent();
0093 
0094             const QSize previewSize = m_model->thumbnailSizeLimit();
0095 
0096             KisChannelsThumbnailsStrokeStrategy *strategy =
0097                 new KisChannelsThumbnailsStrokeStrategy(image->projection(), image->bounds(),
0098                                                         previewSize, false, profile,
0099                                                         renderingIntent, conversionFlags);
0100 
0101             connect(strategy, SIGNAL(thumbnailsUpdated(QVector<QImage>, const KoColorSpace*)), this, SLOT(updateImageThumbnails(QVector<QImage>, const KoColorSpace*)));
0102 
0103             return strategy;
0104         });
0105 }
0106 
0107 void ChannelDockerDock::updateImageThumbnails(const QVector<QImage> &channels, const KoColorSpace *cs)
0108 {
0109     m_model->setChannelThumbnails(channels, cs);
0110     m_channelTable->resizeRowsToContents();
0111     m_channelTable->resizeColumnsToContents();
0112 }
0113 
0114 void ChannelDockerDock::clearCachedState()
0115 {
0116     m_model->setChannelThumbnails({}, nullptr);
0117 }
0118 
0119 
0120 
0121