File: tagcheckbox.cpp

package info (click to toggle)
nepomuk-widgets 4%3A4.13.1-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 908 kB
  • ctags: 386
  • sloc: cpp: 3,302; ansic: 13; makefile: 5; sh: 4
file content (169 lines) | stat: -rw-r--r-- 4,722 bytes parent folder | download
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
   This file is part of the Nepomuk KDE project.
   Copyright (C) 2010 Sebastian Trueg <trueg@kde.org>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) version 3, or any
   later version accepted by the membership of KDE e.V. (or its
   successor approved by the membership of KDE e.V.), which shall
   act as a proxy defined in Section 6 of version 3 of the license.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "tagcheckbox.h"
#include "tagwidget.h"
#include "tagwidget_p.h"

#include <kcursor.h>
#include <kcolorscheme.h>
#include <kglobalsettings.h>

#include <QtGui/QMouseEvent>
#include <QtGui/QStyleOptionButton>
#include <QtGui/QHBoxLayout>
#include <QtGui/QCheckBox>
#include <QtGui/QLabel>


class Nepomuk2::TagCheckBox::CheckBoxWithPublicInitStyleOption : public QCheckBox
{
public:
    CheckBoxWithPublicInitStyleOption( const QString& text, QWidget* parent )
        : QCheckBox( text, parent ) {
    }

    void initStyleOption( QStyleOptionButton* so ) {
        QCheckBox::initStyleOption( so );
    }
};


Nepomuk2::TagCheckBox::TagCheckBox( const Tag& tag, TagWidgetPrivate* tagWidget, QWidget* parent )
    : QWidget( parent ),
      m_label(0),
      m_checkBox(0),
      m_tag(tag),
      m_urlHover(false),
      m_tagWidget(tagWidget)
{
    QHBoxLayout* layout = new QHBoxLayout(this);
    layout->setMargin(0);
    if( tagWidget->m_flags & TagWidget::MiniMode ) {
        m_label = new QLabel( tag.genericLabel(), this );
        m_label->setMouseTracking(true);
        m_label->setTextFormat(Qt::PlainText);
        m_label->setForegroundRole(parent->foregroundRole());
        m_child = m_label;
    }
    else {
        m_checkBox = new CheckBoxWithPublicInitStyleOption( tag.genericLabel(), this );
        m_child = m_checkBox;
        connect(m_checkBox, SIGNAL(stateChanged(int)), this, SLOT(slotStateChanged(int)));
    }
    m_child->installEventFilter( this );
    m_child->setMouseTracking(true);
    layout->addWidget( m_child );
}


Nepomuk2::TagCheckBox::~TagCheckBox()
{
}


bool Nepomuk2::TagCheckBox::isChecked() const
{
    if( m_checkBox )
        return m_checkBox->isChecked();
    else
        return true;
}


void Nepomuk2::TagCheckBox::setChecked( bool checked )
{
    if( m_checkBox )
        m_checkBox->setChecked( checked );
}


void Nepomuk2::TagCheckBox::leaveEvent( QEvent* event )
{
    QWidget::leaveEvent( event );
    enableUrlHover( false );
}


bool Nepomuk2::TagCheckBox::eventFilter( QObject* watched, QEvent* event )
{
    if( watched == m_child ) {
        switch( event->type() ) {
        case QEvent::MouseMove: {
            QMouseEvent* me = static_cast<QMouseEvent*>(event);
            if( !(m_tagWidget->m_flags & TagWidget::DisableTagClicking) )
                enableUrlHover( tagRect().contains(me->pos()) );
        }

        case QEvent::MouseButtonRelease: {
            QMouseEvent* me = static_cast<QMouseEvent*>(event);
            if( !(m_tagWidget->m_flags & TagWidget::DisableTagClicking) &&
                me->button() == Qt::LeftButton &&
                tagRect().contains(me->pos()) ) {
                emit tagClicked( m_tag );
                return true;
            }
        }

        default:
            // do nothing
            break;
        }
    }

    return QWidget::eventFilter( watched, event );
}


void Nepomuk2::TagCheckBox::slotStateChanged( int state )
{
    emit tagStateChanged( m_tag, state );
}


QRect Nepomuk2::TagCheckBox::tagRect() const
{
    if( m_checkBox ) {
        QStyleOptionButton opt;
        m_checkBox->initStyleOption(&opt);
        return style()->subElementRect(QStyle::SE_CheckBoxContents,
                                       &opt,
                                       m_checkBox);
    }
    else {
        return QRect(QPoint(0, 0), m_label->size());
    }
}


void Nepomuk2::TagCheckBox::enableUrlHover( bool enable )
{
    if( m_urlHover != enable ) {
        m_urlHover = enable;
        QFont f = font();
        if(enable)
            f.setUnderline(true);
        m_child->setFont(f);
        m_child->setCursor( enable ? Qt::PointingHandCursor : Qt::ArrowCursor );
    }
}

#include "tagcheckbox.moc"