File: newbasketdialog.cpp

package info (click to toggle)
basket 1.0.2-5
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 14,272 kB
  • ctags: 3,211
  • sloc: cpp: 28,424; sh: 9,518; perl: 2,730; makefile: 235
file content (333 lines) | stat: -rw-r--r-- 12,251 bytes parent folder | download
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/***************************************************************************
 *   Copyright (C) 2003 by S´┐Żastien Laot                                 *
 *   slaout@linux62.org                                                    *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program 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 General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include <kicondialog.h>
#include <qlineedit.h>
#include <kiconview.h>
#include <qlayout.h>
#include <qlabel.h>
#include <klocale.h>
#include <kglobalsettings.h>
#include <kpushbutton.h>
#include <kguiitem.h>
#include <kmessagebox.h>
#include <qsize.h>
#include <qpainter.h>
#include <kapplication.h>
#include <kiconloader.h>
#include <kmainwindow.h>

#include "newbasketdialog.h"
#include "basketfactory.h"
#include "basket.h"
#include "basketlistview.h"
#include "variouswidgets.h"
#include "kcolorcombo2.h"
#include "tools.h"
#include "global.h"
#include "bnpview.h"

/** class SingleSelectionKIconView: */

SingleSelectionKIconView::SingleSelectionKIconView(QWidget *parent, const char *name, WFlags f)
 : KIconView(parent, name, f), m_lastSelected(0)
{
	connect( this, SIGNAL(selectionChanged(QIconViewItem*)), this, SLOT(slotSelectionChanged(QIconViewItem*)) );
	connect( this, SIGNAL(selectionChanged()),               this, SLOT(slotSelectionChanged())               );
}

QDragObject* SingleSelectionKIconView::dragObject()
{
	return 0;
}

void SingleSelectionKIconView::slotSelectionChanged(QIconViewItem *item)
{
	if (item)
		m_lastSelected = item;
}

void SingleSelectionKIconView::slotSelectionChanged()
{
	if (m_lastSelected && !m_lastSelected->isSelected())
		m_lastSelected->setSelected(true);
}

/** class NewBasketDefaultProperties: */

NewBasketDefaultProperties::NewBasketDefaultProperties()
 : icon("")
 , backgroundImage("")
 , backgroundColor()
 , textColor()
 , freeLayout(false)
 , columnCount(1)
{
}

/** class NewBasketDialog: */

NewBasketDialog::NewBasketDialog(Basket *parentBasket, const NewBasketDefaultProperties &defaultProperties, QWidget *parent)
 : KDialogBase(KDialogBase::Swallow, i18n("New Basket"), KDialogBase::Ok | KDialogBase::Cancel,
               KDialogBase::Ok, parent, /*name=*/"NewBasket", /*modal=*/true, /*separator=*/true)
 , m_defaultProperties(defaultProperties)
{
	QWidget *page = new QWidget(this);
	QVBoxLayout *topLayout = new QVBoxLayout(page, /*margin=*/0, spacingHint());

	// Icon, Name and Background Color:
	QHBoxLayout *nameLayout = new QHBoxLayout(0, marginHint()*2/3, spacingHint());
	m_icon = new KIconButton(page);
	m_icon->setIconType(KIcon::NoGroup, KIcon::Action);
	m_icon->setIconSize(16);
	m_icon->setIcon(m_defaultProperties.icon.isEmpty() ? "basket" : m_defaultProperties.icon);
	int size = QMAX(m_icon->sizeHint().width(), m_icon->sizeHint().height());
	m_icon->setFixedSize(size, size); // Make it square!
	QToolTip::add(m_icon, i18n("Icon"));
	m_name = new QLineEdit(/*i18n("Basket"), */page);
	m_name->setMinimumWidth(m_name->fontMetrics().maxWidth()*20);
	connect( m_name, SIGNAL(textChanged(const QString&)), this, SLOT(nameChanged(const QString&)) );
	enableButtonOK(false);
	QToolTip::add(m_name, i18n("Name"));
	m_backgroundColor = new KColorCombo2(QColor(), KGlobalSettings::baseColor(), page);
	m_backgroundColor->setColor(QColor());
	m_backgroundColor->setFixedSize(m_backgroundColor->sizeHint());
	m_backgroundColor->setColor(m_defaultProperties.backgroundColor);
	QToolTip::add(m_backgroundColor, i18n("Background color"));
	nameLayout->addWidget(m_icon);
	nameLayout->addWidget(m_name);
	nameLayout->addWidget(m_backgroundColor);
	topLayout->addLayout(nameLayout);

	QHBoxLayout *layout = new QHBoxLayout(/*parent=*/0, /*margin=*/0, spacingHint());
	KPushButton *button = new KPushButton( KGuiItem(i18n("&Manage Templates..."), "configure"), page );
	connect( button, SIGNAL(clicked()), this, SLOT(manageTemplates()) );
	button->hide();

	// Compute the right template to use as the default:
	QString defaultTemplate = "free";
	if (!m_defaultProperties.freeLayout) {
		if (m_defaultProperties.columnCount == 1)
			defaultTemplate = "1column";
		else if (m_defaultProperties.columnCount == 2)
			defaultTemplate = "2columns";
		else
			defaultTemplate = "3columns";
	}

	// Empty:
	// * * * * *
	// Personnal:
	// *To Do
	// Professionnal:
	// *Meeting Summary
	// Hobbies:
	// *
	m_templates = new SingleSelectionKIconView(page);
	m_templates->setItemsMovable(false);
	m_templates->setMode(KIconView::Select);
	m_templates->setGridX(m_templates->maxItemWidth());
	KIconViewItem *lastTemplate = 0;
	QPixmap icon(40, 53);

	QPainter painter(&icon);
	painter.fillRect(0, 0, icon.width(), icon.height(), KGlobalSettings::baseColor());
	painter.setPen(KGlobalSettings::textColor());
	painter.drawRect(0, 0, icon.width(), icon.height());
	painter.end();
	lastTemplate = new KIconViewItem(m_templates, lastTemplate, i18n("One column"), icon);

	if (defaultTemplate == "1column")
		m_templates->setSelected(lastTemplate, true);

	painter.begin(&icon);
	painter.fillRect(0, 0, icon.width(), icon.height(), KGlobalSettings::baseColor());
	painter.setPen(KGlobalSettings::textColor());
	painter.drawRect(0, 0, icon.width(), icon.height());
	painter.drawLine(icon.width() / 2, 0, icon.width() / 2, icon.height());
	painter.end();
	lastTemplate = new KIconViewItem(m_templates, lastTemplate, i18n("Two columns"), icon);

	if (defaultTemplate == "2columns")
		m_templates->setSelected(lastTemplate, true);

	painter.begin(&icon);
	painter.fillRect(0, 0, icon.width(), icon.height(), KGlobalSettings::baseColor());
	painter.setPen(KGlobalSettings::textColor());
	painter.drawRect(0, 0, icon.width(), icon.height());
	painter.drawLine(icon.width() / 3, 0, icon.width() / 3, icon.height());
	painter.drawLine(icon.width() * 2 / 3, 0, icon.width() * 2 / 3, icon.height());
	painter.end();
	lastTemplate = new KIconViewItem(m_templates, lastTemplate, i18n("Three columns"), icon);

	if (defaultTemplate == "3columns")
		m_templates->setSelected(lastTemplate, true);

	painter.begin(&icon);
	painter.fillRect(0, 0, icon.width(), icon.height(), KGlobalSettings::baseColor());
	painter.setPen(KGlobalSettings::textColor());
	painter.drawRect(0, 0, icon.width(), icon.height());
	painter.drawRect(icon.width() / 5, icon.width() / 5, icon.width() / 4, icon.height() / 8);
	painter.drawRect(icon.width() * 2 / 5, icon.width() * 2 / 5, icon.width() / 4, icon.height() / 8);
	painter.end();
	lastTemplate = new KIconViewItem(m_templates, lastTemplate, i18n("Free"), icon);

	if (defaultTemplate == "free")
		m_templates->setSelected(lastTemplate, true);

/*	painter.begin(&icon);
	painter.fillRect(0, 0, icon.width(), icon.height(), KGlobalSettings::baseColor());
	painter.setPen(KGlobalSettings::textColor());
	painter.drawRect(0, 0, icon.width(), icon.height());
	painter.drawRect(icon.width() * 2 / 5, icon.height() * 3 / 7, icon.width() / 5, icon.height() / 7);
	painter.end();
	lastTemplate = new KIconViewItem(m_templates, lastTemplate, i18n("Mind map"), icon);*/

	m_templates->setMinimumHeight(topLayout->minimumSize().width() * 9 / 16);

	QLabel *label = new QLabel(m_templates, i18n("&Template:"), page);
	layout->addWidget(label, /*stretch=*/0, Qt::AlignBottom);
	layout->addStretch();
	layout->addWidget(button, /*stretch=*/0, Qt::AlignBottom);
	topLayout->addLayout(layout);
	topLayout->addWidget(m_templates);

	layout = new QHBoxLayout(/*parent=*/0, /*margin=*/0, spacingHint());
	m_createIn = new QComboBox(page);
	m_createIn->insertItem(i18n("(Baskets)"));
	label = new QLabel(m_createIn, i18n("C&reate in:"), page);
	HelpLabel *helpLabel = new HelpLabel(i18n("How is it useful?"), i18n(
		"<p>Creating baskets inside of other baskets to form a hierarchy allows you to be more organized by eg.:</p><ul>"
		"<li>Grouping baskets by themes or topics;</li>"
		"<li>Grouping baskets in folders for different projects;</li>"
		"<li>Making sections with sub-baskets representing chapters or pages;</li>"
		"<li>Making a group of baskets to export together (to eg. email them to people).</li></ul>"), page);
	layout->addWidget(label);
	layout->addWidget(m_createIn);
	layout->addWidget(helpLabel);
	layout->addStretch();
	topLayout->addLayout(layout);

	m_basketsMap.clear();
	m_basketsMap.insert(/*index=*/0, /*basket=*/0L);
	populateBasketsList(Global::bnpView->firstListViewItem(), /*indent=*/1, /*index=*/1);

	connect( m_templates, SIGNAL(doubleClicked(QIconViewItem*)), this, SLOT(slotOk())        );
	connect( m_templates, SIGNAL(returnPressed(QIconViewItem*)), this, SLOT(returnPressed()) );

	if (parentBasket) {
		int index = 0;

		for (QMap<int, Basket*>::Iterator it = m_basketsMap.begin(); it != m_basketsMap.end(); ++it)
			if (it.data() == parentBasket) {
				index = it.key();
				break;
			}
		if (index <= 0)
			return;

		if (m_createIn->currentItem() != index)
			m_createIn->setCurrentItem(index);
	}

	setMainWidget(page);
}

void NewBasketDialog::returnPressed()
{
	actionButton(KDialogBase::Ok)->animateClick();
}

int NewBasketDialog::populateBasketsList(QListViewItem *item, int indent, int index)
{
	static const int ICON_SIZE = 16;

	while (item) {
		// Get the basket data:
		Basket *basket = ((BasketListViewItem*)item)->basket();
		QPixmap icon = kapp->iconLoader()->loadIcon(basket->icon(), KIcon::NoGroup, ICON_SIZE, KIcon::DefaultState, 0L, /*canReturnNull=*/false);
		icon = Tools::indentPixmap(icon, indent, 2 * ICON_SIZE / 3);

		// Append item to the list:
		m_createIn->insertItem(icon, basket->basketName());
		m_basketsMap.insert(index, basket);
		++index;

		// Append childs of item to the list:
		index = populateBasketsList(item->firstChild(), indent + 1, index);

		// Add next sibling basket:
		item = item->nextSibling();
	}

	return index;
}

NewBasketDialog::~NewBasketDialog()
{
}

void NewBasketDialog::polish()
{
	KDialogBase::polish();
	m_name->setFocus();
}

void NewBasketDialog::nameChanged(const QString &newName)
{
	enableButtonOK(!newName.isEmpty());
}

void NewBasketDialog::slotOk()
{
	QIconViewItem *item = ((SingleSelectionKIconView*)m_templates)->selectedItem();
	QString templateName;
	if (item->text() == i18n("One column"))
		templateName = "1column";
	if (item->text() == i18n("Two columns"))
		templateName = "2columns";
	if (item->text() == i18n("Three columns"))
		templateName = "3columns";
	if (item->text() == i18n("Free-form"))
		templateName = "free";
	if (item->text() == i18n("Mind map"))
		templateName = "mindmap";

	Global::bnpView->closeAllEditors();

	QString backgroundImage;
	QColor  textColor;
	if (m_backgroundColor->color() == m_defaultProperties.backgroundColor) {
		backgroundImage = m_defaultProperties.backgroundImage;
		textColor       = m_defaultProperties.textColor;
	}

	BasketFactory::newBasket(m_icon->icon(), m_name->text(), backgroundImage, m_backgroundColor->color(), textColor, templateName, m_basketsMap[m_createIn->currentItem()]);
	if(Global::mainWindow()) Global::mainWindow()->show();

	KDialogBase::slotOk();
}

void NewBasketDialog::manageTemplates()
{
	KMessageBox::information(this, "Wait a minute! There is no template for now: they will come with time... :-D");
}

#include "newbasketdialog.moc"