1
/****************************************************************************
3
** This file is part of the LibreCAD project, a 2D CAD program
5
** Copyright (C) 2010 R. van Twisk (librecad@rvt.dds.nl)
6
** Copyright (C) 2001-2003 RibbonSoft. All rights reserved.
9
** This file may be distributed and/or modified under the terms of the
10
** GNU General Public License version 2 as published by the Free Software
11
** Foundation and appearing in the file gpl-2.0.txt included in the
12
** packaging of this file.
14
** This program is distributed in the hope that it will be useful,
15
** but WITHOUT ANY WARRANTY; without even the implied warranty of
16
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
** GNU General Public License for more details.
19
** You should have received a copy of the GNU General Public License
20
** along with this program; if not, write to the Free Software
21
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23
** This copyright notice MUST APPEAR in all copies of the script!
25
**********************************************************************/
29
#include "rs_blocklist.h"
31
#include "rs_blocklistlistener.h"
33
#if QT_VERSION < 0x040400
40
* @param owner true if this is the owner of the blocks added.
41
* If so, the blocks will be deleted when the block
44
RS_BlockList::RS_BlockList(bool owner) {
46
//blocks.setAutoDelete(owner);
53
* Removes all blocks in the blocklist.
55
void RS_BlockList::clear() {
63
* Activates the given block.
64
* Listeners are notified.
66
void RS_BlockList::activate(const QString& name) {
67
RS_DEBUG->print("RS_BlockList::activateBlock");
73
* Activates the given block.
74
* Listeners are notified.
76
void RS_BlockList::activate(RS_Block* block) {
77
RS_DEBUG->print("RS_BlockList::activateBlock");
81
for (unsigned i=0; i<blockListListeners.count(); ++i) {
82
RS_BlockListListener* l = blockListListeners.at(i);
84
l->blockActivated(activeBlock);
92
* Adds a block to the block list. If a block with the same name
93
* exists already, the given block will be deleted if the blocklist
96
* @param notify true if you want listeners to be notified.
98
* @return false: block already existed and was deleted.
100
bool RS_BlockList::add(RS_Block* block, bool notify) {
101
RS_DEBUG->print("RS_BlockList::add()");
107
// check if block already exists:
108
RS_Block* b = find(block->getName());
110
blocks.append(block);
131
* Notifies the listeners about blocks that were added. This can be
132
* used after adding a lot of blocks without auto-update or simply
133
* to force an update of GUI blocklists.
135
void RS_BlockList::addNotification() {
136
for (int i=0; i<blockListListeners.size(); ++i) {
137
RS_BlockListListener* l = blockListListeners.at(i);
145
* Removes a block from the list.
146
* Listeners are notified after the block was removed from
147
* the list but before it gets deleted.
149
void RS_BlockList::remove(RS_Block* block) {
150
RS_DEBUG->print("RS_BlockList::removeBlock()");
152
// here the block is removed from the list but not deleted
153
#if QT_VERSION < 0x040400
154
emu_qt44_removeOne(blocks, block);
156
blocks.removeOne(block);
159
for (int i=0; i<blockListListeners.size(); ++i) {
160
RS_BlockListListener* l = blockListListeners.at(i);
161
l->blockRemoved(block);
167
// activate an other block if necessary:
168
if (activeBlock==block) {
169
//activate(blocks.first());
174
// now it's save to delete the block
183
* Tries to rename the given block to 'name'. Block names are unique in the
186
* @retval true block was successfully renamed.
187
* @retval false block couldn't be renamed.
189
bool RS_BlockList::rename(RS_Block* block, const QString& name) {
191
if (find(name)==NULL) {
192
block->setName(name);
203
* Changes a block's attributes. The attributes of block 'block'
204
* are copied from block 'source'.
205
* Listeners are notified.
208
void RS_BlockList::editBlock(RS_Block* block, const RS_Block& source) {
211
for (unsigned i=0; i<blockListListeners.count(); ++i) {
212
RS_BlockListListener* l = blockListListeners.at(i);
214
l->blockEdited(block);
222
* @return Pointer to the block with the given name or
223
* \p NULL if no such block was found.
225
RS_Block* RS_BlockList::find(const QString& name) {
226
//RS_DEBUG->print("RS_BlockList::find");
227
RS_Block* ret = NULL;
228
// Todo : reduce this from O(N) to O(log(N)) complexity based on sorted list or hash
229
for (int i=0; i<count(); ++i) {
231
if (b->getName()==name) {
243
* Finds a new unique block name.
245
* @param suggestion Suggested name the new name will be based on.
247
QString RS_BlockList::newName(const QString& suggestion) {
249
for (int i=0; i<1e5; ++i) {
250
name = QString("%1-%2").arg(suggestion).arg(i);
251
if (find(name)==NULL) {
262
* Switches on / off the given block.
263
* Listeners are notified.
265
void RS_BlockList::toggle(const QString& name) {
272
* Switches on / off the given block.
273
* Listeners are notified.
275
void RS_BlockList::toggle(RS_Block* block) {
281
// TODO LordOfBikes: when block attributes are saved, activate this
285
for (int i=0; i<blockListListeners.size(); ++i) {
286
RS_BlockListListener* l = blockListListeners.at(i);
288
l->blockToggled(block);
295
* Freezes or defreezes all blocks.
297
* @param freeze true: freeze, false: defreeze
299
void RS_BlockList::freezeAll(bool freeze) {
301
for (int l=0; l<count(); l++) {
302
at(l)->freeze(freeze);
304
// TODO LordOfBikes: when block attributes are saved, activate this
307
for (int i=0; i<blockListListeners.size(); ++i) {
308
RS_BlockListListener* l = blockListListeners.at(i);
309
l->blockToggled(NULL);
316
* Switches on / off the given block.
317
* Listeners are notified.
320
void RS_BlockList::toggleBlock(const QString& name) {
321
RS_Block* block = findBlock(name);
325
for (unsigned i=0; i<blockListListeners.count(); ++i) {
326
RS_BlockListListener* l = blockListListeners.at(i);
328
l->blockToggled(block);
335
* adds a BlockListListener to the list of listeners. Listeners
336
* are notified when the block list changes.
338
void RS_BlockList::addListener(RS_BlockListListener* listener) {
339
blockListListeners.append(listener);
345
* removes a BlockListListener from the list of listeners.
347
void RS_BlockList::removeListener(RS_BlockListListener* listener) {
348
#if QT_VERSION < 0x040400
349
emu_qt44_removeOne(blockListListeners, listener);
351
blockListListeners.removeOne(listener);
358
* Dumps the blocks to stdout.
360
std::ostream& operator << (std::ostream& os, RS_BlockList& b) {
362
os << "Blocklist: \n";
363
for (int i=0; i<b.count(); ++i) {
364
RS_Block* blk = b.at(i);