2
This program is free software; you can redistribute it and/or modify
3
it under the terms of the GNU General Public License as published by
4
the Free Software Foundation; either version 2 of the License, or
5
(at your option) any later version.
9
Copyright (C) 2009 Bernd Buschinski <b.buschinski@web.de>
12
#include "whiteboardtoolbar.h"
14
#include <QHBoxLayout>
17
#include <QImageWriter>
20
#include <KPushButton>
22
#include <KFileDialog>
26
#include "whiteboardfontchooser.h"
28
namespace Konversation
32
WhiteBoardToolBar::WhiteBoardToolBar(QWidget* parent)
34
m_lineWidthPixmap(20, 20),
35
m_textType(SimpleText),
40
m_clearPushButton->setIcon(KIcon("document-edit"));
41
m_clearPushButton->setToolTip(i18n("Clear Image"));
42
m_savePushButton->setIcon(KIcon("document-save"));
43
m_savePushButton->setToolTip(i18n("Save As.."));
45
m_pencilPushButton->setIcon(KIcon("draw-freehand"));
46
m_pencilPushButton->setToolTip(i18n("Freehand Drawing"));
47
m_pencilPushButton->setFlat(true);
48
m_toggleButtonHash.insert(m_pencilPushButton, WhiteBoardGlobals::Pencil);
49
m_pencilPushButton->setChecked(true);
51
m_linePushButton->setIcon(KIcon("draw-line"));
52
m_linePushButton->setToolTip(i18n("Draw a straight line"));
53
m_linePushButton->setFlat(true);
54
m_toggleButtonHash.insert(m_linePushButton, WhiteBoardGlobals::Line);
56
m_rectanglePushButton->setIcon(KIcon("draw-rectangle"));
57
m_rectanglePushButton->setToolTip(i18n("Draw a rectangle"));
58
m_rectanglePushButton->setFlat(true);
59
m_toggleButtonHash.insert(m_rectanglePushButton, WhiteBoardGlobals::Rectangle);
60
m_toggleButtonHash.insert(m_rectanglePushButton, WhiteBoardGlobals::FilledRectangle);
62
m_ellipsePushButton->setIcon(KIcon("draw-circle"));
63
m_ellipsePushButton->setToolTip(i18n("Draw an ellipse"));
64
m_ellipsePushButton->setFlat(true);
65
m_toggleButtonHash.insert(m_ellipsePushButton, WhiteBoardGlobals::Ellipse);
66
m_toggleButtonHash.insert(m_ellipsePushButton, WhiteBoardGlobals::FilledEllipse);
68
m_textPushButton->setIcon(KIcon("draw-text"));
69
m_textPushButton->setToolTip(i18n("Draw text"));
70
m_textPushButton->setFlat(true);
71
m_toggleButtonHash.insert(m_textPushButton, WhiteBoardGlobals::Text);
73
m_selectionPushButton->setEnabled(false); // it has no function in current whiteboard
74
m_selectionPushButton->setIcon(KIcon("select-rectangular"));
75
m_selectionPushButton->setToolTip(i18nc("dcc whiteboard selection tool", "Selection"));
76
m_selectionPushButton->setFlat(true);
77
m_toggleButtonHash.insert(m_selectionPushButton, WhiteBoardGlobals::Selection);
79
m_eraserPushButton->setIcon(KIcon("draw-eraser"));
80
m_eraserPushButton->setToolTip(i18n("Eraser"));
81
m_eraserPushButton->setFlat(true);
82
m_toggleButtonHash.insert(m_eraserPushButton, WhiteBoardGlobals::Eraser);
84
m_fillPushButton->setIcon(KIcon("fill-color"));
85
m_fillPushButton->setToolTip(i18n("Fill a contiguous area with the foreground color"));
86
m_fillPushButton->setFlat(true);
87
m_toggleButtonHash.insert(m_fillPushButton, WhiteBoardGlobals::FloodFill);
89
m_arrowPushButton->setIcon(KIcon("draw-arrow-forward"));
90
m_arrowPushButton->setToolTip(i18n("Draw an arrow"));
91
m_arrowPushButton->setFlat(true);
92
m_toggleButtonHash.insert(m_arrowPushButton, WhiteBoardGlobals::Arrow);
94
m_lineWidthSlider->setMaximum(WhiteBoardGlobals::MaxPenWidth);
96
connectToggleButtons();
98
//foreward colorchooser signals
99
connect(m_colorChooser, SIGNAL(colorsSwapped(const QColor&,const QColor&)),
100
this, SIGNAL(colorsSwapped(const QColor&,const QColor&)));
101
connect(m_colorChooser, SIGNAL(foregroundColorChanged(const QColor&)),
102
this, SIGNAL(foregroundColorChanged(const QColor&)));
103
connect(m_colorChooser, SIGNAL(backgroundColorChanged(const QColor&)),
104
this, SIGNAL(backgroundColorChanged(const QColor&)));
106
connect(m_lineWidthSlider, SIGNAL(valueChanged(int)),
107
this, SIGNAL(lineWidthChanged(int)));
108
connect(m_lineWidthSlider, SIGNAL(valueChanged(int)),
109
this, SLOT(updateLineWidthPixmap(int)));
111
connect(m_clearPushButton, SIGNAL(clicked()), this, SLOT(clearClicked()));
112
connect(m_savePushButton, SIGNAL(clicked()), this, SLOT(saveClicked()));
114
setFormOptionVisible(false);
115
setLineWidthVisible(true);
116
updateLineWidthPixmap(1);
119
WhiteBoardToolBar::~WhiteBoardToolBar()
124
QColor WhiteBoardToolBar::foregroundColor() const
126
return m_colorChooser->foregroundColor();
129
QColor WhiteBoardToolBar::backgroundColor() const
131
return m_colorChooser->backgroundColor();
134
void WhiteBoardToolBar::disableTool(WhiteBoardGlobals::WhiteBoardTool tool)
136
KPushButton* button = m_toggleButtonHash.key(tool);
139
button->setEnabled(false);
143
kDebug() << "unhandled tool:" << tool;
147
void WhiteBoardToolBar::enableTool(WhiteBoardGlobals::WhiteBoardTool tool)
149
KPushButton* button = m_toggleButtonHash.key(tool);
152
button->setEnabled(true);
156
kDebug() << "unhandled tool:" << tool;
160
void WhiteBoardToolBar::setSupportedTextType(WhiteBoardToolBar::TextType textType)
162
m_textType = textType;
163
if (m_textType == WhiteBoardToolBar::ExtentedText)
169
m_fontDialog = new WhiteBoardFontChooser(this);
170
connect(m_fontDialog, SIGNAL(fontChanged(const QFont&)),
171
this, SIGNAL(fontChanged(QFont)));
177
disconnect(m_fontDialog, 0, 0, 0);
184
WhiteBoardToolBar::TextType WhiteBoardToolBar::textType() const
189
void WhiteBoardToolBar::connectToggleButtons()
192
connect(m_pencilPushButton, SIGNAL(toggled(bool)), this, SLOT(pencilToggled(bool)));
193
connect(m_linePushButton, SIGNAL(toggled(bool)), this, SLOT(lineToggled(bool)));
194
connect(m_rectanglePushButton, SIGNAL(toggled(bool)), this, SLOT(rectangleToggled(bool)));
195
connect(m_ellipsePushButton, SIGNAL(toggled(bool)), this, SLOT(ellipseToggled(bool)));
196
connect(m_textPushButton, SIGNAL(toggled(bool)), this, SLOT(textToggled(bool)));
197
connect(m_selectionPushButton, SIGNAL(toggled(bool)), this, SLOT(selectionToggled(bool)));
198
connect(m_eraserPushButton, SIGNAL(toggled(bool)), this, SLOT(eraseToggled(bool)));
199
connect(m_fillPushButton, SIGNAL(toggled(bool)), this, SLOT(fillToggled(bool)));
200
connect(m_arrowPushButton, SIGNAL(toggled(bool)), this, SLOT(arrowToggled(bool)));
203
void WhiteBoardToolBar::disconnectToggleButtons()
206
disconnect(m_pencilPushButton, 0, 0, 0);
207
disconnect(m_linePushButton, 0, 0, 0);
208
disconnect(m_rectanglePushButton, 0, 0, 0);
209
disconnect(m_ellipsePushButton, 0, 0, 0);
210
disconnect(m_textPushButton, 0, 0, 0);
211
disconnect(m_selectionPushButton, 0, 0, 0);
212
disconnect(m_eraserPushButton, 0, 0, 0);
213
disconnect(m_fillPushButton, 0, 0, 0);
214
disconnect(m_arrowPushButton, 0, 0, 0);
217
void WhiteBoardToolBar::clearClicked()
219
//TODO ask for confirm
223
void WhiteBoardToolBar::saveClicked()
225
QPointer<KFileDialog> fileDialog = new KFileDialog(KUrl(QDir::homePath()), "*.png\n*.jpg", this);
226
fileDialog->setCaption(i18n("Save Image"));
227
fileDialog->setOperationMode(KFileDialog::Saving);
228
fileDialog->setMode(KFile::File);
229
int ret = fileDialog->exec();
231
if (ret == KDialog::Accepted && fileDialog)
233
kDebug() << fileDialog->selectedFile();
234
emit save(fileDialog->selectedFile());
239
void WhiteBoardToolBar::arrowToggled (bool checked)
241
handleToggleButton(m_arrowPushButton, checked, WhiteBoardGlobals::Arrow);
242
setLineWidthVisible(true);
243
setFormOptionVisible(false);
244
setFontDialogVisible(false);
247
void WhiteBoardToolBar::ellipseToggled(bool checked)
249
handleToggleButton(m_ellipsePushButton, checked, WhiteBoardGlobals::Ellipse);
250
setLineWidthVisible(true);
251
setFormOptionVisible(true);
252
fillFormOptionList(Ellipse);
253
setFontDialogVisible(false);
256
void WhiteBoardToolBar::eraseToggled(bool checked)
258
handleToggleButton(m_eraserPushButton, checked, WhiteBoardGlobals::Eraser);
259
setLineWidthVisible(true);
260
setFormOptionVisible(false);
261
setFontDialogVisible(false);
264
void WhiteBoardToolBar::fillToggled(bool checked)
266
handleToggleButton(m_fillPushButton, checked, WhiteBoardGlobals::FloodFill);
267
setLineWidthVisible(false);
268
setFormOptionVisible(false);
269
setFontDialogVisible(false);
272
void WhiteBoardToolBar::lineToggled(bool checked)
274
handleToggleButton(m_linePushButton, checked, WhiteBoardGlobals::Line);
275
setLineWidthVisible(true);
276
setFormOptionVisible(false);
277
setFontDialogVisible(false);
280
void WhiteBoardToolBar::pencilToggled(bool checked)
282
handleToggleButton(m_pencilPushButton, checked, WhiteBoardGlobals::Pencil);
283
setLineWidthVisible(true);
284
setFormOptionVisible(false);
285
setFontDialogVisible(false);
288
void WhiteBoardToolBar::rectangleToggled(bool checked)
290
handleToggleButton(m_rectanglePushButton, checked, WhiteBoardGlobals::Rectangle);
291
setLineWidthVisible(true);
292
setFormOptionVisible(true);
293
fillFormOptionList(Rectangle);
294
setFontDialogVisible(false);
297
void WhiteBoardToolBar::selectionToggled(bool checked)
299
handleToggleButton(m_selectionPushButton, checked, WhiteBoardGlobals::Selection);
300
setLineWidthVisible(false);
301
setFormOptionVisible(false);
302
setFontDialogVisible(false);
305
void WhiteBoardToolBar::textToggled(bool checked)
307
if (textType() == WhiteBoardToolBar::SimpleText)
309
handleToggleButton(m_textPushButton, checked, WhiteBoardGlobals::Text);
313
handleToggleButton(m_textPushButton, checked, WhiteBoardGlobals::TextExtended);
315
setLineWidthVisible(false);
316
setFormOptionVisible(false);
317
setFontDialogVisible(true);
320
void WhiteBoardToolBar::handleToggleButton(KPushButton* button, bool checked, Konversation::DCC::WhiteBoardGlobals::WhiteBoardTool tool)
322
disconnectToggleButtons();
323
kDebug() << "tool:" << tool << "checked:" << checked;
326
unCheckOtherButtons(button);
327
emit toolChanged(tool);
331
// don't uncheck the button
332
button->setChecked(true);
334
connectToggleButtons();
337
void WhiteBoardToolBar::unCheckOtherButtons(KPushButton* button)
339
foreach (KPushButton* pushButton, m_toggleButtonHash.keys())
341
if (pushButton != button && pushButton->isChecked())
343
pushButton->setChecked(false);
349
void WhiteBoardToolBar::updateLineWidthPixmap(int lineWidth)
351
if (m_lineWidthLabel->width() != m_lineWidthPixmap.width()-2 ||
352
m_lineWidthLabel->height() != m_lineWidthPixmap.height())
354
m_lineWidthPixmap = QPixmap(m_lineWidthLabel->width()-2, m_lineWidthLabel->height());
356
//hm.. really white? or is transparent better?
357
m_lineWidthPixmap.fill(Qt::white);
359
QPainter tPaint(&m_lineWidthPixmap);
360
tPaint.setPen(QPen(Qt::black, lineWidth));
361
tPaint.drawLine(0, m_lineWidthPixmap.height()/2, m_lineWidthPixmap.width(), m_lineWidthPixmap.height()/2);
363
m_lineWidthLabel->setPixmap(m_lineWidthPixmap);
366
void WhiteBoardToolBar::formSelectionChanged()
369
QList<QListWidgetItem *> selectList = m_formOptionListWidget->selectedItems();
370
const int selectedRow = m_formOptionListWidget->row(selectList.first());
371
if (selectedRow == 0)
373
if (m_rectanglePushButton->isChecked())
375
kDebug() << "emit rectangle";
376
emit toolChanged(WhiteBoardGlobals::Rectangle);
378
else if (m_ellipsePushButton->isChecked())
380
kDebug() << "emit ellipse";
381
emit toolChanged(WhiteBoardGlobals::Ellipse);
384
else if (selectedRow == 1)
386
if (m_rectanglePushButton->isChecked())
388
kDebug() << "emit filledrectangle";
389
emit toolChanged(WhiteBoardGlobals::FilledRectangle);
391
else if (m_ellipsePushButton->isChecked())
393
kDebug() << "emit filledellipse";
394
emit toolChanged(WhiteBoardGlobals::FilledEllipse);
399
void WhiteBoardToolBar::setLineWidthVisible(bool visible)
401
m_lineWidthFrame->setVisible(visible);
404
void WhiteBoardToolBar::setFormOptionVisible(bool visible)
406
m_formOptionListWidget->setVisible(visible);
409
void WhiteBoardToolBar::setFontDialogVisible(bool visible)
411
if (m_textType == WhiteBoardToolBar::ExtentedText && m_fontDialog)
414
m_fontDialog->show();
416
m_fontDialog->hide();
420
void WhiteBoardToolBar::fillFormOptionList(FormOption form)
422
disconnect(m_formOptionListWidget, 0, 0, 0);
423
m_formOptionListWidget->clear();
424
const int width = m_formOptionListWidget->contentsRect().width() - m_formOptionListWidget->lineWidth()*4 - 3;
425
const int drawHeight = 20 - 2;
426
const int widthLayoutOffset = 2;
427
const QSize sizeHint(width, 20);
428
// kDebug() << "wanted width" << width;
429
// kDebug() << "actual width" << m_formOptionListWidget->contentsRect().width();
434
if (m_rectanglePixmap.width() != width ||
435
m_rectanglePixmap.height() != 20)
437
m_rectanglePixmap = QPixmap(width, 20);
438
m_rectanglePixmap.fill(Qt::transparent);
439
m_filledRectanglePixmap = QPixmap(width, 20);
440
m_filledRectanglePixmap.fill(Qt::transparent);
441
QPainter tPaint(&m_rectanglePixmap);
442
tPaint.drawRect(0, 0, width-widthLayoutOffset, drawHeight);
444
tPaint.begin(&m_filledRectanglePixmap);
445
tPaint.setBrush(Qt::black);
446
tPaint.drawRect(0, 0, width-widthLayoutOffset, drawHeight);
449
QListWidgetItem *tRectangle = new QListWidgetItem("", m_formOptionListWidget, QListWidgetItem::UserType +1);
450
tRectangle->setData(Qt::DecorationRole, QVariant(m_rectanglePixmap));
451
tRectangle->setSizeHint(sizeHint);
453
QListWidgetItem *tFilledRectangle = new QListWidgetItem("", m_formOptionListWidget, QListWidgetItem::UserType +1);
454
tFilledRectangle->setData(Qt::DecorationRole, QVariant(m_filledRectanglePixmap));
455
tFilledRectangle->setSizeHint(sizeHint);
457
tRectangle->setSelected(true);
462
if (m_ellipsePixmap.width() != width ||
463
m_ellipsePixmap.height() != 20)
465
m_ellipsePixmap = QPixmap(width, 20);
466
m_ellipsePixmap.fill(Qt::transparent);
467
m_filledEllipsePixmap = QPixmap(width, 20);
468
m_filledEllipsePixmap.fill(Qt::transparent);
469
QPainter tPaint(&m_ellipsePixmap);
470
tPaint.drawEllipse(0, 0, width-widthLayoutOffset, drawHeight);
472
tPaint.begin(&m_filledEllipsePixmap);
473
tPaint.setBrush(Qt::black);
474
tPaint.drawEllipse(0, 0, width-widthLayoutOffset, drawHeight);
477
QListWidgetItem *tEllipse = new QListWidgetItem("", m_formOptionListWidget, QListWidgetItem::UserType +1);
478
tEllipse->setData(Qt::DecorationRole, QVariant(m_ellipsePixmap));
479
tEllipse->setSizeHint(sizeHint);
481
QListWidgetItem *tFilledEllipse = new QListWidgetItem("", m_formOptionListWidget, QListWidgetItem::UserType +1);
482
tFilledEllipse->setData(Qt::DecorationRole, QVariant(m_filledEllipsePixmap));
483
tFilledEllipse->setSizeHint(sizeHint);
485
tEllipse->setSelected(true);
489
connect(m_formOptionListWidget, SIGNAL(itemSelectionChanged()),
490
this, SLOT(formSelectionChanged()));