3 // Part of the Architektonas Project
4 // Originally part of QCad Community Edition by Andrew Mustun
5 // Extensively rewritten and refactored by James L. Hammons
6 // Portions copyright (C) 2001-2003 RibbonSoft
7 // Copyright (C) 2010 Underground Software
8 // See the README and GPLv2 files for licensing and warranty information
10 // JLH = James L. Hammons <jlhamm@acm.org>
13 // --- ---------- -----------------------------------------------------------
14 // JLH 05/28/2010 Added this text. :-)
17 #include "rs_blocklist.h"
22 * @param owner true if this is the owner of the blocks added.
23 * If so, the blocks will be deleted when the block
26 RS_BlockList::RS_BlockList(bool owner)
29 //blocks.setAutoDelete(owner);
30 //OK, this is good. We don't have to fuck with this shit.
31 // blocks.setAutoDelete(false);
32 // blockListListeners.setAutoDelete(false);
37 /*virtual*/ RS_BlockList::~RS_BlockList()
42 * Removes all blocks in the blocklist.
44 void RS_BlockList::clear()
51 * @return Number of blocks available.
53 uint RS_BlockList::count()
55 return blocks.count();
59 * @return Block at given position or NULL if i is out of range.
61 RS_Block * RS_BlockList::at(uint i)
67 * Activates the given block.
68 * Listeners are notified.
70 void RS_BlockList::activate(const QString & name)
72 RS_DEBUG->print("RS_BlockList::activateBlock");
78 * Activates the given block.
79 * Listeners are notified.
81 void RS_BlockList::activate(RS_Block * block)
83 RS_DEBUG->print("RS_BlockList::activateBlock");
87 for (uint i=0; i<blockListListeners.count(); ++i) {
88 RS_BlockListListener* l = blockListListeners.at(i);
90 l->blockActivated(activeBlock);
96 //! @return The active block of NULL if no block is activated.
97 RS_Block * RS_BlockList::getActive()
103 * Adds a block to the block list. If a block with the same name
104 * exists already, the given block will be deleted if the blocklist
107 * @param notify true if you want listeners to be notified.
109 * @return false: block already existed and was deleted.
111 bool RS_BlockList::add(RS_Block * block, bool notify)
113 RS_DEBUG->print("RS_BlockList::add()");
118 // check if block already exists:
119 RS_Block * b = find(block->getName());
123 blocks.append(block);
145 * Notifies the listeners about blocks that were added. This can be
146 * used after adding a lot of blocks without auto-update or simply
147 * to force an update of GUI blocklists.
149 void RS_BlockList::addNotification()
151 for(int i=0; i<blockListListeners.count(); ++i)
153 RS_BlockListListener * l = blockListListeners.at(i);
159 * Removes a block from the list.
160 * Listeners are notified after the block was removed from
161 * the list but before it gets deleted.
163 void RS_BlockList::remove(RS_Block * block)
165 RS_DEBUG->print("RS_BlockList::removeBlock()");
167 // here the block is removed from the list but not deleted
168 // blocks.remove(block);
169 blocks.removeAll(block);
171 for(int i=0; i<blockListListeners.count(); ++i)
173 RS_BlockListListener * l = blockListListeners.at(i);
174 l->blockRemoved(block);
180 // activate an other block if necessary:
181 if (activeBlock == block)
183 //activate(blocks.first());
188 // now it's safe to delete the block
194 * Tries to rename the given block to 'name'. Block names are unique in the
197 * @retval true block was successfully renamed.
198 * @retval false block couldn't be renamed.
200 bool RS_BlockList::rename(RS_Block * block, const QString & name)
204 if (find(name) == NULL)
206 block->setName(name);
216 * Changes a block's attributes. The attributes of block 'block'
217 * are copied from block 'source'.
218 * Listeners are notified.
221 void RS_BlockList::editBlock(RS_Block* block, const RS_Block& source) {
224 for (uint i=0; i<blockListListeners.count(); ++i) {
225 RS_BlockListListener* l = blockListListeners.at(i);
227 l->blockEdited(block);
233 * @return Pointer to the block with the given name or
234 * \p NULL if no such block was found.
236 RS_Block * RS_BlockList::find(const QString & name)
238 //RS_DEBUG->print("RS_BlockList::find");
240 for(uint i=0; i<count(); i++)
242 RS_Block * b = at(i);
244 if (b->getName() == name)
252 * Finds a new unique block name.
254 * @param suggestion Suggested name the new name will be based on.
256 QString RS_BlockList::newName(const QString & suggestion)
260 for(int i=0; i<1e5; ++i)
262 name = QString("%1-%2").arg(suggestion).arg(i);
264 if (find(name) == NULL)
272 * Switches on / off the given block.
273 * Listeners are notified.
275 void RS_BlockList::toggle(const QString & name)
281 * Switches on / off the given block.
282 * Listeners are notified.
284 void RS_BlockList::toggle(RS_Block * block)
292 for(int i=0; i<blockListListeners.count(); ++i)
294 RS_BlockListListener * l = blockListListeners.at(i);
296 l->blockToggled(block);
301 * Freezes or defreezes all blocks.
303 * @param freeze true: freeze, false: defreeze
305 void RS_BlockList::freezeAll(bool freeze)
307 for(uint l=0; l<count(); l++)
308 at(l)->freeze(freeze);
310 for(int i=0; i<blockListListeners.count(); ++i)
312 RS_BlockListListener * l = blockListListeners.at(i);
313 l->blockToggled(NULL);
318 * Switches on / off the given block.
319 * Listeners are notified.
322 void RS_BlockList::toggleBlock(const QString& name) {
323 RS_Block* block = findBlock(name);
327 for (uint i=0; i<blockListListeners.count(); ++i) {
328 RS_BlockListListener* l = blockListListeners.at(i);
330 l->blockToggled(block);
336 * adds a BlockListListener to the list of listeners. Listeners
337 * are notified when the block list changes.
339 void RS_BlockList::addListener(RS_BlockListListener * listener)
341 blockListListeners.append(listener);
345 * removes a BlockListListener from the list of listeners.
347 void RS_BlockList::removeListener(RS_BlockListListener * listener)
349 // blockListListeners.remove(listener);
350 blockListListeners.removeAll(listener);
354 * Sets the layer lists modified status to 'm'.
356 void RS_BlockList::setModified(bool m)
362 * @retval true The layer list has been modified.
363 * @retval false The layer list has not been modified.
365 /*virtual*/ bool RS_BlockList::isModified() const
371 * Dumps the blocks to stdout.
373 std::ostream & operator<<(std::ostream & os, RS_BlockList & b)
375 os << "Blocklist: \n";
377 for(uint i=0; i<b.count(); ++i)
379 RS_Block * blk = b.at(i);