File indexing completed on 2024-06-16 04:16:02
0001 /* 0002 * SPDX-FileCopyrightText: 2010 Adam Celarek <kdedev at xibo dot at> 0003 * 0004 * SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include "kis_shade_selector_line_combo_box.h" 0008 0009 #include <QApplication> 0010 #include <QScreen> 0011 #include <QDesktopWidget> 0012 #include <QResizeEvent> 0013 #include <QGridLayout> 0014 #include <QPainter> 0015 0016 #include <klocalizedstring.h> 0017 #include <qnamespace.h> 0018 0019 #include "kis_shade_selector_line.h" 0020 #include "kis_shade_selector_line_combo_box_popup.h" 0021 #include "kis_color_selector_base_proxy.h" 0022 0023 #include "kis_global.h" 0024 0025 0026 KisShadeSelectorLineComboBox::KisShadeSelectorLineComboBox(QWidget *parent) : 0027 QComboBox(parent), 0028 m_popup(new KisShadeSelectorLineComboBoxPopup(this)), 0029 m_parentProxy(new KisColorSelectorBaseProxyNoop()), 0030 m_currentLine(new KisShadeSelectorLine(0,0,0, m_parentProxy.data(), this)) 0031 { 0032 QGridLayout* l = new QGridLayout(this); 0033 { 0034 int left; 0035 l->getContentsMargins(&left, nullptr, nullptr, nullptr); 0036 l->setContentsMargins(left, 0, 30, 0); 0037 0038 } 0039 l->addWidget(m_currentLine); 0040 0041 m_currentLine->setAttribute(Qt::WA_TransparentForMouseEvents); 0042 0043 KoColor color; 0044 color.fromQColor(QColor(190, 50, 50)); 0045 m_currentLine->setColor(color); 0046 0047 updateSettings(); 0048 } 0049 0050 KisShadeSelectorLineComboBox::~KisShadeSelectorLineComboBox() 0051 { 0052 } 0053 0054 void KisShadeSelectorLineComboBox::hidePopup() 0055 { 0056 QComboBox::hidePopup(); 0057 m_popup->hide(); 0058 } 0059 0060 void KisShadeSelectorLineComboBox::showPopup() 0061 { 0062 QComboBox::showPopup(); 0063 m_popup->show(); 0064 0065 const int widgetMargin = 20; 0066 #if (QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)) 0067 QScreen *screen = qApp->screenAt(QCursor::pos()); 0068 QRect fitRect; 0069 if (screen) { 0070 fitRect = kisGrowRect(screen->availableGeometry(), -widgetMargin); 0071 } 0072 else { 0073 fitRect = kisGrowRect(QRect(0, 0, 1024, 768), -widgetMargin); 0074 } 0075 #else 0076 QRect fitRect = kisGrowRect(QApplication::desktop()->screenGeometry(), -widgetMargin); 0077 #endif 0078 QRect popupRect = m_popup->rect(); 0079 popupRect.moveTo(mapToGlobal(QPoint())); 0080 popupRect = kisEnsureInRect(popupRect, fitRect); 0081 0082 m_popup->move(popupRect.topLeft()); 0083 m_popup->setConfiguration(m_currentLine->toString()); 0084 } 0085 0086 void KisShadeSelectorLineComboBox::setConfiguration(const QString &stri) 0087 { 0088 m_currentLine->fromString(stri); 0089 update(); 0090 } 0091 0092 QString KisShadeSelectorLineComboBox::configuration() const 0093 { 0094 return m_currentLine->toString(); 0095 } 0096 0097 void KisShadeSelectorLineComboBox::setLineNumber(int n) 0098 { 0099 m_currentLine->setLineNumber(n); 0100 for(int i=0; i<m_popup->layout()->count(); i++) { 0101 KisShadeSelectorLine* item = dynamic_cast<KisShadeSelectorLine*>(m_popup->layout()->itemAt(i)->widget()); 0102 if(item!=nullptr) { 0103 item->setLineNumber(n); 0104 } 0105 } 0106 } 0107 0108 void KisShadeSelectorLineComboBox::resizeEvent(QResizeEvent *e) 0109 { 0110 e->accept(); 0111 0112 m_popup->setMinimumWidth(qMax(280, width())); 0113 m_popup->setMaximumWidth(qMax(280, width())); 0114 } 0115 0116 void KisShadeSelectorLineComboBox::updateSettings() 0117 { 0118 m_currentLine->updateSettings(); 0119 for(int i=0; i<m_popup->layout()->count(); i++) { 0120 KisShadeSelectorLine* item = dynamic_cast<KisShadeSelectorLine*>(m_popup->layout()->itemAt(i)->widget()); 0121 if(item!=nullptr) { 0122 item->updateSettings(); 0123 item->m_lineHeight = 30; 0124 item->setMaximumHeight(30); 0125 item->setMinimumHeight(30); 0126 } 0127 } 0128 0129 setLineHeight(m_currentLine->m_lineHeight); 0130 } 0131 0132 0133 void KisShadeSelectorLineComboBox::setGradient(bool b) 0134 { 0135 m_currentLine->m_gradient=b; 0136 for(int i=0; i<m_popup->layout()->count(); i++) { 0137 KisShadeSelectorLine* item = dynamic_cast<KisShadeSelectorLine*>(m_popup->layout()->itemAt(i)->widget()); 0138 if(item!=nullptr) { 0139 item->m_gradient=b; 0140 } 0141 } 0142 0143 update(); 0144 } 0145 0146 void KisShadeSelectorLineComboBox::setPatches(bool b) 0147 { 0148 m_currentLine->m_gradient=!b; 0149 for(int i=0; i<m_popup->layout()->count(); i++) { 0150 KisShadeSelectorLine* item = dynamic_cast<KisShadeSelectorLine*>(m_popup->layout()->itemAt(i)->widget()); 0151 if(item!=nullptr) { 0152 item->m_gradient=!b; 0153 } 0154 } 0155 0156 update(); 0157 } 0158 0159 void KisShadeSelectorLineComboBox::setPatchCount(int count) 0160 { 0161 m_currentLine->m_patchCount=count; 0162 for(int i=0; i<m_popup->layout()->count(); i++) { 0163 KisShadeSelectorLine* item = dynamic_cast<KisShadeSelectorLine*>(m_popup->layout()->itemAt(i)->widget()); 0164 if(item!=nullptr) { 0165 item->m_patchCount=count; 0166 } 0167 } 0168 0169 update(); 0170 } 0171 0172 void KisShadeSelectorLineComboBox::setLineHeight(int height) 0173 { 0174 m_currentLine->m_lineHeight=height; 0175 m_currentLine->setMinimumHeight(height); 0176 setMinimumHeight(height+m_popup->spacing); 0177 0178 update(); 0179 }