1/****************************************************************************
2** Copyright (C) 2001-2012 Klaralvdalens Datakonsult AB. All rights reserved.
3**
4** This file is part of the KD Tools library.
5**
6** Licensees holding valid commercial KD Tools licenses may use this file in
7** accordance with the KD Tools Commercial License Agreement provided with
8** the Software.
9**
10**
11** This file may be distributed and/or modified under the terms of the GNU
12** Lesser General Public License version 2 and version 3 as published by the
13** Free Software Foundation and appearing in the file LICENSE.LGPL included.
14**
15** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
17**
18** Contact info@kdab.net if any conditions of this licensing are not
19** clear to you.
20**
21**********************************************************************/
22
23#include "kdsignalblocker.h"
24
25#include <QObject>
26
27using namespace Akonadi;
28
29/*!
30 \class KDSignalBlocker
31 \ingroup raii core
32 \brief Exception-safe and convenient wrapper around QObject::blockSignals()
33
34 All methods in this class are nothrow if QObject::blockSignals() and
35 QObject::signalsBlocked() are nothrow, which they normally are.
36*/
37
38/*!
39 Constructor. Blocks signals on \a o.
40
41 \post o->signalsBlocked() == true
42*/
43KDSignalBlocker::KDSignalBlocker(QObject *o)
44 : wasBlocked(o->signalsBlocked())
45 , object(o)
46{
47 o->blockSignals(true);
48}
49
50/*!
51 \overload
52
53 \post o.signalsBlocked() == true
54*/
55KDSignalBlocker::KDSignalBlocker(QObject &o)
56 : wasBlocked(o.signalsBlocked())
57 , object(&o)
58{
59 o.blockSignals(true);
60}
61
62/*!
63 Destructor. Unblocks signals (unless they were blocked before), if not already
64 done by unblock().
65
66 \post o->signalsBlocked() is the same as just before this instance has been constructed.
67*/
68KDSignalBlocker::~KDSignalBlocker()
69{
70 unblock();
71}
72
73/*
74 Unblocks signals (unless they were blocked before).
75 You can use reblock() to block them again.
76 There is no need to reblock before destruction.
77
78 \post o->signalsBlocked() is the same as just before this instance has been constructed.
79*/
80void KDSignalBlocker::unblock()
81{
82 object->blockSignals(wasBlocked);
83}
84
85/*
86 Unblocks signals (unless they were blocked before)
87 \post o->signalsBlocked() is the same as just before this instance has been constructed.
88*/
89void KDSignalBlocker::reblock()
90{
91 object->blockSignals(true);
92}
93