Pencil2D  ff90c0872e88be3bf81c548cd60f01983012ec49
Pencil2D is an animation software for both bitmap and vector graphics. It is free, multi-platform, and open source.
 All Classes Functions
popupcolorpalettewidget.cpp
1 
2 #include "popupcolorpalettewidget.h"
3 
4 #include <QBoxLayout>
5 #include <QGraphicsDropShadowEffect>
6 #include <QPushButton>
7 #include <QKeyEvent>
8 #include "colorbox.h"
9 #include "editor.h"
10 #include "scribblearea.h"
11 
12 
13 PopupColorPaletteWidget::PopupColorPaletteWidget( ScribbleArea *parent ) :
14  QWidget ( parent, Qt::Window ),
15  mContainer ( parent )
16 {
17  QVBoxLayout* mainLayout = new QVBoxLayout( this );
18 
19  setVisible( false );
20  setFixedWidth(200);
21 
22  mColorBox = new ColorBox();
23  mainLayout->addWidget(mColorBox);
24  mColorBox->adjustSize();
25 
26  QGraphicsDropShadowEffect* effect = new QGraphicsDropShadowEffect();
27  effect->setXOffset(2);
28  effect->setYOffset(2);
29  effect->setBlurRadius(5);
30  setGraphicsEffect(effect);
31 
32  setAutoFillBackground(true);
33  setWindowTitle("Color palette");
34  setWindowFlags( ( (windowFlags()
35  | Qt::CustomizeWindowHint)
36  & ~Qt::WindowMaximizeButtonHint
37  & ~Qt::WindowMinimizeButtonHint) );
38 
39  // --- bottom buttons layout ---
40  QHBoxLayout *buttonsLayout = new QHBoxLayout();
41  mainLayout->addLayout(buttonsLayout);
42  closeButton = new QPushButton(this);
43  closeButton->setText("close/toggle");
44  buttonsLayout->addWidget(closeButton);
45 
46  // --- connections ---
47  connect( closeButton , SIGNAL( clicked() ) , mContainer , SLOT( togglePopupPalette() ) );
48  connect( mColorBox, SIGNAL( colorChanged(QColor) ), this, SLOT( onColorChanged(QColor) ) );
49 }
50 
51 void PopupColorPaletteWidget::popup()
52 {
53  if ( this->isVisible() )
54  {
55  //color = m_colorBox->color();
56  hide();
57  return;
58  }
59  // opening palette
60  mColorBox->setColor( mContainer->editor()->color()->frontColor() );
61  mColorBox->setFocus();
62 
63  QPoint cPos = QCursor::pos();
64  int radius = width() / 2;
65 
66  cPos.setX(cPos.x()-radius); // adjust cPos to center widget
67  cPos.setY(cPos.y()-radius);
68 
69  move( cPos );
70  show();
71  return;
72 }
73 
74 void PopupColorPaletteWidget::keyPressEvent(QKeyEvent *event)
75 {
76  if (event->key() == Qt::Key_Enter)
77  {
78  mColorBox->setFocus();
79  qDebug() << "sent key_enter";
80  return;
81  }
82  else if (event->key() == Qt::Key_Escape)
83  {
84  close();
85  }
86  else
87  {
88  event->ignore();
89  qDebug() << "sent event.ignore()";
90  QCoreApplication::sendEvent(mContainer, event);
91  }
92 }
93 
94 // --------------- slots ---------------
95 
96 void PopupColorPaletteWidget::onColorChanged(const QColor& color)
97 {
98  mContainer->editor()->color()->setColor( color );
99 }