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 // (C) 2010 Underground Software
8 // JLH = James L. Hammons <jlhamm@acm.org>
11 // --- ---------- -----------------------------------------------------------
12 // JLH 05/28/2010 Added this text. :-)
15 #include "rs_blocklist.h"
20 * @param owner true if this is the owner of the blocks added.
21 * If so, the blocks will be deleted when the block
24 RS_BlockList::RS_BlockList(bool owner)
27 //blocks.setAutoDelete(owner);
28 //OK, this is good. We don't have to fuck with this shit.
29 // blocks.setAutoDelete(false);
30 // blockListListeners.setAutoDelete(false);
35 /*virtual*/ RS_BlockList::~RS_BlockList()
40 * Removes all blocks in the blocklist.
42 void RS_BlockList::clear()
49 * @return Number of blocks available.
51 uint RS_BlockList::count()
53 return blocks.count();
57 * @return Block at given position or NULL if i is out of range.
59 RS_Block * RS_BlockList::at(uint i)
65 * Activates the given block.
66 * Listeners are notified.
68 void RS_BlockList::activate(const QString & name)
70 RS_DEBUG->print("RS_BlockList::activateBlock");
76 * Activates the given block.
77 * Listeners are notified.
79 void RS_BlockList::activate(RS_Block * block)
81 RS_DEBUG->print("RS_BlockList::activateBlock");
85 for (uint i=0; i<blockListListeners.count(); ++i) {
86 RS_BlockListListener* l = blockListListeners.at(i);
88 l->blockActivated(activeBlock);
94 //! @return The active block of NULL if no block is activated.
95 RS_Block * RS_BlockList::getActive()
101 * Adds a block to the block list. If a block with the same name
102 * exists already, the given block will be deleted if the blocklist
105 * @param notify true if you want listeners to be notified.
107 * @return false: block already existed and was deleted.
109 bool RS_BlockList::add(RS_Block * block, bool notify)
111 RS_DEBUG->print("RS_BlockList::add()");
116 // check if block already exists:
117 RS_Block * b = find(block->getName());
121 blocks.append(block);
143 * Notifies the listeners about blocks that were added. This can be
144 * used after adding a lot of blocks without auto-update or simply
145 * to force an update of GUI blocklists.
147 void RS_BlockList::addNotification()
149 for(int i=0; i<blockListListeners.count(); ++i)
151 RS_BlockListListener * l = blockListListeners.at(i);
157 * Removes a block from the list.
158 * Listeners are notified after the block was removed from
159 * the list but before it gets deleted.
161 void RS_BlockList::remove(RS_Block * block)
163 RS_DEBUG->print("RS_BlockList::removeBlock()");
165 // here the block is removed from the list but not deleted
166 // blocks.remove(block);
167 blocks.removeAll(block);
169 for(int i=0; i<blockListListeners.count(); ++i)
171 RS_BlockListListener * l = blockListListeners.at(i);
172 l->blockRemoved(block);
178 // activate an other block if necessary:
179 if (activeBlock == block)
181 //activate(blocks.first());
186 // now it's safe to delete the block
192 * Tries to rename the given block to 'name'. Block names are unique in the
195 * @retval true block was successfully renamed.
196 * @retval false block couldn't be renamed.
198 bool RS_BlockList::rename(RS_Block * block, const QString & name)
202 if (find(name) == NULL)
204 block->setName(name);
214 * Changes a block's attributes. The attributes of block 'block'
215 * are copied from block 'source'.
216 * Listeners are notified.
219 void RS_BlockList::editBlock(RS_Block* block, const RS_Block& source) {
222 for (uint i=0; i<blockListListeners.count(); ++i) {
223 RS_BlockListListener* l = blockListListeners.at(i);
225 l->blockEdited(block);
231 * @return Pointer to the block with the given name or
232 * \p NULL if no such block was found.
234 RS_Block * RS_BlockList::find(const QString & name)
236 //RS_DEBUG->print("RS_BlockList::find");
237 RS_Block * ret = NULL;
239 for(uint i=0; i<count(); ++i)
241 RS_Block * b = at(i);
243 if (b->getName() == name)
254 * Finds a new unique block name.
256 * @param suggestion Suggested name the new name will be based on.
258 QString RS_BlockList::newName(const QString & suggestion)
262 for(int i=0; i<1e5; ++i)
264 name = QString("%1-%2").arg(suggestion).arg(i);
266 if (find(name) == NULL)
274 * Switches on / off the given block.
275 * Listeners are notified.
277 void RS_BlockList::toggle(const QString & name)
283 * Switches on / off the given block.
284 * Listeners are notified.
286 void RS_BlockList::toggle(RS_Block * block)
294 for(int i=0; i<blockListListeners.count(); ++i)
296 RS_BlockListListener * l = blockListListeners.at(i);
298 l->blockToggled(block);
303 * Freezes or defreezes all blocks.
305 * @param freeze true: freeze, false: defreeze
307 void RS_BlockList::freezeAll(bool freeze)
309 for(uint l=0; l<count(); l++)
310 at(l)->freeze(freeze);
312 for(int i=0; i<blockListListeners.count(); ++i)
314 RS_BlockListListener * l = blockListListeners.at(i);
315 l->blockToggled(NULL);
320 * Switches on / off the given block.
321 * Listeners are notified.
324 void RS_BlockList::toggleBlock(const QString& name) {
325 RS_Block* block = findBlock(name);
329 for (uint i=0; i<blockListListeners.count(); ++i) {
330 RS_BlockListListener* l = blockListListeners.at(i);
332 l->blockToggled(block);
338 * adds a BlockListListener to the list of listeners. Listeners
339 * are notified when the block list changes.
341 void RS_BlockList::addListener(RS_BlockListListener * listener)
343 blockListListeners.append(listener);
347 * removes a BlockListListener from the list of listeners.
349 void RS_BlockList::removeListener(RS_BlockListListener * listener)
351 // blockListListeners.remove(listener);
352 blockListListeners.removeAll(listener);
356 * Sets the layer lists modified status to 'm'.
358 void RS_BlockList::setModified(bool m)
364 * @retval true The layer list has been modified.
365 * @retval false The layer list has not been modified.
367 /*virtual*/ bool RS_BlockList::isModified() const
373 * Dumps the blocks to stdout.
375 std::ostream & operator<<(std::ostream & os, RS_BlockList & b)
377 os << "Blocklist: \n";
379 for(uint i=0; i<b.count(); ++i)
381 RS_Block * blk = b.at(i);