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"
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 RS_BlockList::RS_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*/ RS_BlockList::~RS_BlockList()
44 * Removes all blocks in the blocklist.
46 void RS_BlockList::clear()
53 * @return Number of blocks available.
55 uint RS_BlockList::count()
57 return blocks.count();
61 * @return Block at given position or NULL if i is out of range.
63 RS_Block * RS_BlockList::at(uint i)
69 * Activates the given block.
70 * Listeners are notified.
72 void RS_BlockList::activate(const QString & name)
74 RS_DEBUG->print("RS_BlockList::activateBlock");
80 * Activates the given block.
81 * Listeners are notified.
83 void RS_BlockList::activate(RS_Block * block)
85 RS_DEBUG->print("RS_BlockList::activateBlock");
89 for (uint i=0; i<blockListListeners.count(); ++i) {
90 RS_BlockListListener* l = blockListListeners.at(i);
92 l->blockActivated(activeBlock);
98 //! @return The active block of NULL if no block is activated.
99 RS_Block * RS_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 RS_BlockList::add(RS_Block * block, bool notify)
115 RS_DEBUG->print("RS_BlockList::add()");
120 // check if block already exists:
121 RS_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 RS_BlockList::addNotification()
154 for(int i=0; i<blockListListeners.count(); ++i)
156 RS_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 RS_BlockList::remove(RS_Block * block)
169 RS_DEBUG->print("RS_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 RS_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 RS_BlockList::rename(RS_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 RS_BlockList::editBlock(RS_Block* block, const RS_Block& source) {
230 for (uint i=0; i<blockListListeners.count(); ++i) {
231 RS_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 RS_Block * RS_BlockList::find(const QString & name)
244 //RS_DEBUG->print("RS_BlockList::find");
246 for(uint i=0; i<count(); i++)
248 RS_Block * b = at(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 RS_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 RS_BlockList::toggle(const QString & name)
287 * Switches on / off the given block.
288 * Listeners are notified.
290 void RS_BlockList::toggle(RS_Block * block)
299 for(int i=0; i<blockListListeners.count(); ++i)
301 RS_BlockListListener * l = blockListListeners.at(i);
303 l->blockToggled(block);
309 * Freezes or defreezes all blocks.
311 * @param freeze true: freeze, false: defreeze
313 void RS_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 RS_BlockListListener * l = blockListListeners.at(i);
322 l->blockToggled(NULL);
328 * Switches on / off the given block.
329 * Listeners are notified.
332 void RS_BlockList::toggleBlock(const QString& name) {
333 RS_Block* block = findBlock(name);
337 for (uint i=0; i<blockListListeners.count(); ++i) {
338 RS_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 RS_BlockList::addListener(RS_BlockListListener * listener)
352 blockListListeners.append(listener);
356 * removes a BlockListListener from the list of listeners.
358 void RS_BlockList::removeListener(RS_BlockListListener * listener)
360 blockListListeners.removeAll(listener);
365 * Sets the layer lists modified status to 'm'.
367 void RS_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 RS_BlockList::isModified() const
382 * Dumps the blocks to stdout.
384 std::ostream & operator<<(std::ostream & os, RS_BlockList & b)
386 os << "Blocklist: \n";
388 for(uint i=0; i<b.count(); ++i)
390 RS_Block * blk = b.at(i);