summaryrefslogtreecommitdiffstats
path: root/kmail/kcursorsaver.h
blob: 930cab6f02e2d999665d0225bf00bd0f881949c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#ifndef kcursorsaver_h
#define kcursorsaver_h

#include <qcursor.h>
#include <qapplication.h>

/**
 * @short sets a cursor and makes sure it's restored on destruction
 * Create a KCursorSaver object when you want to set the cursor.
 * As soon as it gets out of scope, it will restore the original
 * cursor.
 */
class KCursorSaver : public Qt
{
public:
    /// constructor taking QCursor shapes
    KCursorSaver(Qt::CursorShape shape) {
        QApplication::setOverrideCursor( QCursor(shape) );
        inited = true;
    }

    /// copy constructor. The right side won't restore the cursor
    KCursorSaver( const KCursorSaver &rhs ) {
        *this = rhs;
    }

    /// restore the cursor
    ~KCursorSaver() {
        if (inited)
            QApplication::restoreOverrideCursor();
    }

    /// call this to explitly restore the cursor
    inline void restoreCursor(void) {
        QApplication::restoreOverrideCursor();
        inited = false;
    }

protected:
    void operator=( const KCursorSaver &rhs ) {
        inited = rhs.inited;
        rhs.inited = false;
    }

private:
    mutable bool inited;
};

/**
 * convenience functions
 */
namespace KBusyPtr {
    inline KCursorSaver idle() {
        return KCursorSaver(QCursor::ArrowCursor);
    }
    inline KCursorSaver busy() {
        return KCursorSaver(QCursor::WaitCursor);
    }
}

#endif /*kbusyptr_h_*/