All Classes Namespaces Functions Variables Enumerations Properties Pages
popupcolorpalettewidget.cpp
1 /*
2 
3 Pencil2D - Traditional Animation Software
4 Copyright (C) 2005-2007 Patrick Corrieri & Pascal Naidon
5 Copyright (C) 2012-2020 Matthew Chiawen Chang
6 
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 as published by the Free Software Foundation; version 2 of the License.
10 
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15 
16 */
17 #include "popupcolorpalettewidget.h"
18 
19 #include <QBoxLayout>
20 #include <QGraphicsDropShadowEffect>
21 #include <QPushButton>
22 #include <QKeyEvent>
23 #include "colorbox.h"
24 #include "editor.h"
25 #include "scribblearea.h"
26 
27 
28 PopupColorPaletteWidget::PopupColorPaletteWidget( ScribbleArea *parent ) :
29  QWidget ( parent, Qt::Window ),
30  mContainer ( parent )
31 {
32  QVBoxLayout* mainLayout = new QVBoxLayout( this );
33 
34  setVisible( false );
35  setFixedWidth(200);
36 
37  mColorBox = new ColorBox();
38  mainLayout->addWidget(mColorBox);
39  mColorBox->adjustSize();
40 
42  effect->setXOffset(2);
43  effect->setYOffset(2);
44  effect->setBlurRadius(5);
45  setGraphicsEffect(effect);
46 
47  setAutoFillBackground(true);
48  setWindowTitle(tr("Color palette"));
49  setWindowFlags( ( (windowFlags()
53 
54  // --- bottom buttons layout ---
55  QHBoxLayout *buttonsLayout = new QHBoxLayout();
56  mainLayout->addLayout(buttonsLayout);
57  closeButton = new QPushButton(this);
58  closeButton->setText(tr("close/toggle"));
59  buttonsLayout->addWidget(closeButton);
60 
61  // --- connections ---
62  connect( closeButton , &QPushButton::clicked , mContainer , &ScribbleArea::togglePopupPalette );
63  connect( mColorBox, &ColorBox::colorChanged, this, &PopupColorPaletteWidget::onColorChanged );
64 }
65 
66 void PopupColorPaletteWidget::popup()
67 {
68  if ( this->isVisible() )
69  {
70  //color = m_colorBox->color();
71  hide();
72  return;
73  }
74  // opening palette
75  mColorBox->setColor( mContainer->editor()->color()->frontColor() );
76  mColorBox->setFocus();
77 
78  QPoint cPos = QCursor::pos();
79  int radius = width() / 2;
80 
81  cPos.setX(cPos.x()-radius); // adjust cPos to center widget
82  cPos.setY(cPos.y()-radius);
83 
84  move( cPos );
85  show();
86  return;
87 }
88 
89 void PopupColorPaletteWidget::keyPressEvent(QKeyEvent *event)
90 {
91  if (event->key() == Qt::Key_Enter)
92  {
93  mColorBox->setFocus();
94  qDebug() << "sent key_enter";
95  return;
96  }
97  else if (event->key() == Qt::Key_Escape)
98  {
99  close();
100  }
101  else
102  {
103  event->ignore();
104  qDebug() << "sent event.ignore()";
105  QCoreApplication::sendEvent(mContainer, event);
106  }
107 }
108 
109 // --------------- slots ---------------
110 
111 void PopupColorPaletteWidget::onColorChanged(const QColor& color)
112 {
113  mContainer->editor()->color()->setColor( color );
114 }
bool close()
void setXOffset(qreal dx)
bool isVisible() const const
QString tr(const char *sourceText, const char *disambiguation, int n)
int x() const const
int y() const const
int width() const const
void addWidget(QWidget *widget, int stretch, Qt::Alignment alignment)
void setFocus()
void clicked(bool checked)
bool sendEvent(QObject *receiver, QEvent *event)
void hide()
int key() const const
QColor frontColor(bool useIndexedColor=true)
frontColor
void move(int x, int y)
QPoint pos()
void setX(int x)
void setY(int y)
void setYOffset(qreal dy)
void show()
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
Key_Enter
void setBlurRadius(qreal blurRadius)
void addLayout(QLayout *layout, int stretch)