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 "blocklist.h"
24 * @param owner true if this is the owner of the blocks added.
25 * If so, the blocks will be deleted when the block
28 BlockList::BlockList(bool owner)
31 //blocks.setAutoDelete(owner);
32 //OK, this is good. We don't have to fuck with this shit.
33 // blocks.setAutoDelete(false);
34 // blockListListeners.setAutoDelete(false);
39 /*virtual*/ BlockList::~BlockList()
44 * Removes all blocks in the blocklist.
46 void BlockList::clear()
53 * @return Number of blocks available.
55 uint BlockList::count()
57 return blocks.count();
61 * @return Block at given position or NULL if i is out of range.
63 Block * BlockList::at(uint i)
69 * Activates the given block.
70 * Listeners are notified.
72 void BlockList::activate(const QString & name)
74 DEBUG->print("BlockList::activateBlock");
80 * Activates the given block.
81 * Listeners are notified.
83 void BlockList::activate(Block * block)
85 DEBUG->print("BlockList::activateBlock");
89 for (uint i=0; i<blockListListeners.count(); ++i) {
90 BlockListListener* l = blockListListeners.at(i);
92 l->blockActivated(activeBlock);
98 //! @return The active block of NULL if no block is activated.
99 Block * BlockList::getActive()
105 * Adds a block to the block list. If a block with the same name
106 * exists already, the given block will be deleted if the blocklist
109 * @param notify true if you want listeners to be notified.
111 * @return false: block already existed and was deleted.
113 bool BlockList::add(Block * block, bool notify)
115 DEBUG->print("BlockList::add()");
120 // check if block already exists:
121 Block * b = find(block->getName());
125 blocks.append(block);
147 * Notifies the listeners about blocks that were added. This can be
148 * used after adding a lot of blocks without auto-update or simply
149 * to force an update of GUI blocklists.
151 void BlockList::addNotification()
154 for(int i=0; i<blockListListeners.count(); ++i)
156 BlockListListener * l = blockListListeners.at(i);
163 * Removes a block from the list.
164 * Listeners are notified after the block was removed from
165 * the list but before it gets deleted.
167 void BlockList::remove(Block * block)
169 DEBUG->print("BlockList::removeBlock()");
171 // here the block is removed from the list but not deleted
172 // blocks.remove(block);
173 blocks.removeAll(block);
176 for(int i=0; i<blockListListeners.count(); ++i)
178 BlockListListener * l = blockListListeners.at(i);
179 l->blockRemoved(block);
186 // activate an other block if necessary:
187 if (activeBlock == block)
189 //activate(blocks.first());
194 // now it's safe to delete the block
200 * Tries to rename the given block to 'name'. Block names are unique in the
203 * @retval true block was successfully renamed.
204 * @retval false block couldn't be renamed.
206 bool BlockList::rename(Block * block, const QString & name)
210 if (find(name) == NULL)
212 block->setName(name);
222 * Changes a block's attributes. The attributes of block 'block'
223 * are copied from block 'source'.
224 * Listeners are notified.
227 void BlockList::editBlock(Block* block, const Block& source) {
230 for (uint i=0; i<blockListListeners.count(); ++i) {
231 BlockListListener* l = blockListListeners.at(i);
233 l->blockEdited(block);
239 * @return Pointer to the block with the given name or
240 * \p NULL if no such block was found.
242 Block * BlockList::find(const QString & name)
244 //DEBUG->print("BlockList::find");
246 for(uint i=0; i<count(); i++)
250 if (b->getName() == name)
258 * Finds a new unique block name.
260 * @param suggestion Suggested name the new name will be based on.
262 QString BlockList::newName(const QString & suggestion)
266 for(int i=0; i<1e5; ++i)
268 name = QString("%1-%2").arg(suggestion).arg(i);
270 if (find(name) == NULL)
278 * Switches on / off the given block.
279 * Listeners are notified.
281 void BlockList::toggle(const QString & name)
287 * Switches on / off the given block.
288 * Listeners are notified.
290 void BlockList::toggle(Block * block)
299 for(int i=0; i<blockListListeners.count(); ++i)
301 BlockListListener * l = blockListListeners.at(i);
303 l->blockToggled(block);
309 * Freezes or defreezes all blocks.
311 * @param freeze true: freeze, false: defreeze
313 void BlockList::freezeAll(bool freeze)
315 for(uint l=0; l<count(); l++)
316 at(l)->freeze(freeze);
319 for(int i=0; i<blockListListeners.count(); ++i)
321 BlockListListener * l = blockListListeners.at(i);
322 l->blockToggled(NULL);
328 * Switches on / off the given block.
329 * Listeners are notified.
332 void BlockList::toggleBlock(const QString& name) {
333 Block* block = findBlock(name);
337 for (uint i=0; i<blockListListeners.count(); ++i) {
338 BlockListListener* l = blockListListeners.at(i);
340 l->blockToggled(block);
346 * adds a BlockListListener to the list of listeners. Listeners
347 * are notified when the block list changes.
350 void BlockList::addListener(BlockListListener * listener)
352 blockListListeners.append(listener);
356 * removes a BlockListListener from the list of listeners.
358 void BlockList::removeListener(BlockListListener * listener)
360 blockListListeners.removeAll(listener);
365 * Sets the layer lists modified status to 'm'.
367 void BlockList::setModified(bool m)
373 * @retval true The layer list has been modified.
374 * @retval false The layer list has not been modified.
376 /*virtual*/ bool BlockList::isModified() const
382 * Dumps the blocks to stdout.
384 std::ostream & operator<<(std::ostream & os, BlockList & b)
386 os << "Blocklist: \n";
388 for(uint i=0; i<b.count(); ++i)
390 Block * blk = b.at(i);