forked from Ramblurr/PietCreator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CommandWidget.cpp
177 lines (148 loc) · 7.33 KB
/
CommandWidget.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
Copyright (C) 2010 Casey Link <unnamedrambler@gmail.com>
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 3 of the License, or (at your
option) any later version.
This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
#include "CommandWidget.h"
#include "KColorCells.h"
#include "KColorPatch.h"
#include "ViewMonitor.h"
#include "CommandsModel.h"
#include "CommandDelegate.h"
#include "Command.h"
#include <QBoxLayout>
#include <QHeaderView>
#include <QEvent>
#include <QMouseEvent>
#include <QLabel>
#include <QDebug>
CommandWidget::CommandWidget( ViewMonitor* monitor, QWidget* parent ): QWidget( parent ), mMonitor( monitor )
{
QBoxLayout *mainLayout = new QVBoxLayout( this );
setLayout( mainLayout );
setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
QWidget *colorsWidget = new QWidget( this );
QHBoxLayout *hlayout = new QHBoxLayout( colorsWidget );
mPalette = new KColorCells( colorsWidget, 6, 3 );
mPalette->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
mPalette->setFixedSize( 25 * 3, 25 * 6 );
mMonitor->populateCells( mPalette );
KColorCells* mBWPalette = new KColorCells( colorsWidget, 2, 1 );
mBWPalette->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
mBWPalette->setFixedSize( 25, 25 * 6 );
mBWPalette->setColor( 0, Qt::black );
mBWPalette->setColor( 1, Qt::white );
const int spacing = 15;
QWidget *currentCommandWidget = new QWidget( colorsWidget );
QVBoxLayout *vlayout = new QVBoxLayout( currentCommandWidget );
QWidget *patchWidget = new QWidget( currentCommandWidget );
patchWidget->setSizePolicy( QSizePolicy::Minimum, QSizePolicy::Minimum );
patchWidget->setFixedWidth( 48 + spacing );
mSecondaryPatch = new KColorPatch( patchWidget );
mSecondaryPatch->setFixedSize( 48, 48 );
mSecondaryPatch->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
mSecondaryPatch->installEventFilter( this );
mPrimaryPatch = new KColorPatch( patchWidget );
mPrimaryPatch->setFixedSize( 48, 48 );
mPrimaryPatch->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
QRect secRect = mPrimaryPatch->geometry();
secRect.setTopLeft( QPoint( secRect.topLeft().x() + spacing, secRect.topLeft().y() + spacing ) );
mSecondaryPatch->setGeometry( secRect );
mPrimaryCommandLabel = new QLabel( currentCommandWidget );
mPrimaryCommandLabel->setSizePolicy( QSizePolicy::Minimum, QSizePolicy::Minimum );
mSecondaryCommandLabel = new QLabel( currentCommandWidget );
mSecondaryCommandLabel->setAlignment( Qt::AlignRight );
mSecondaryCommandLabel->setSizePolicy( QSizePolicy::Minimum, QSizePolicy::Minimum );
patchWidget->setFixedHeight( 48 + spacing );
vlayout->addWidget( mPrimaryCommandLabel );
vlayout->addWidget( patchWidget );
vlayout->addWidget( mSecondaryCommandLabel );
vlayout->addStretch();
currentCommandWidget->setLayout( vlayout );
hlayout->setSpacing( 0 );
hlayout->addStretch();
hlayout->addWidget( currentCommandWidget );
hlayout->addWidget( mPalette );
hlayout->addWidget( mBWPalette );
hlayout->addStretch();
colorsWidget->setLayout( hlayout );
mCommandsModel = new CommandsModel( mMonitor, this );
mCommandsView = new QTableView( this );
mCommandsView->horizontalHeader()->hide();
mCommandsView->verticalHeader()->hide();
mCommandsView->horizontalHeader()->setSectionResizeMode( QHeaderView::ResizeToContents );
mCommandsView->setModel( mCommandsModel );
mCommandDelegate = new CommandDelegate( mMonitor, this );
mCommandsView->setItemDelegate( mCommandDelegate );
mainLayout->insertWidget( 0, colorsWidget );
mainLayout->insertWidget( 1, mCommandsView );
mainLayout->addStretch();
connect( mPalette, SIGNAL( colorSelected( int, QColor ) ), mMonitor, SLOT( setCurrentColor( int, QColor ) ) );
connect( mPalette, SIGNAL( colorSelected( int, QColor ) ), mBWPalette, SLOT( clearSelection( ) ) );
connect( mBWPalette, SIGNAL( colorSelected( int, QColor ) ), mMonitor, SLOT( setCurrentColor( int, QColor ) ) );
connect( mBWPalette, SIGNAL( colorSelected( int, QColor ) ), mPalette, SLOT( clearSelection( ) ) );
connect( mMonitor, SIGNAL( currentCommandChanged( Command, Command ) ), mCommandsView, SLOT( reset() ) );
connect( mMonitor, SIGNAL( currentColorChanged( QColor ) ), mCommandsView, SLOT( reset() ) );
connect( mMonitor, SIGNAL( currentColorChanged( QColor ) ), mPrimaryPatch, SLOT( setColor( QColor ) ) );
connect( mMonitor, SIGNAL( currentCommandChanged( Command, Command ) ), mPalette, SLOT( clearSelection( ) ) );
connect( mMonitor, SIGNAL( currentCommandChanged( Command, Command ) ), mBWPalette, SLOT( clearSelection( ) ) );
connect( mMonitor, SIGNAL( currentCommandChanged( Command, Command ) ), this, SLOT( slotCurrentCommandChanged( Command, Command ) ) );
connect( mCommandsView, SIGNAL( clicked( QModelIndex ) ), this, SLOT( slotCommandClicked( QModelIndex ) ) );
connect( mCommandsView->horizontalHeader(), SIGNAL( geometriesChanged() ), this, SLOT( slotSetViewWidth() ) );
Command firstcmd = mCommandsModel->data( mCommandsModel->index( 0, 0 ), CommandsModel::CommandRole ).value<Command>();
mMonitor->setCurrentCommand( firstcmd );
slotSetViewWidth();
}
CommandWidget::~CommandWidget()
{
}
void CommandWidget::slotCurrentCommandChanged( const Command& newCommand, const Command& oldCommand )
{
mPrimaryPatch->setColor( newCommand.color );
mPrimaryCommandLabel->setText( newCommand.name );
mSecondaryPatch->setColor( oldCommand.color );
mSecondaryCommandLabel->setText( oldCommand.name );
}
void CommandWidget::slotCommandClicked( const QModelIndex& index )
{
Command command = index.data( CommandsModel::CommandRole ).value<Command>();
qDebug() << "command clicked:" << command.name << command.index;
mMonitor->setCurrentCommand( command );
}
bool CommandWidget::eventFilter( QObject* obj, QEvent* event )
{
if ( obj == mSecondaryPatch && event->type() == QEvent::MouseButtonRelease ) {
QMouseEvent * mevent = static_cast<QMouseEvent*>( event );
if ( mevent->button() == Qt::LeftButton ) {
mMonitor->takeCommand();
return true;
}
}
return QObject::eventFilter( obj, event );
}
void CommandWidget::slotSetViewWidth()
{
// This table will always have 3 columns.
// if this changes we have bigger worries
mCommandsView->resizeColumnsToContents();
int width = 0;
width += mCommandsView->columnWidth( 0 );
width += mCommandsView->columnWidth( 1 );
width += mCommandsView->columnWidth( 2 );
width += mCommandsView->frameWidth();
width += 3;
mCommandsView->setMinimumWidth( width );
mCommandsView->adjustSize();
adjustSize();
}
#include "CommandWidget.moc"