summaryrefslogtreecommitdiffstats
path: root/src/libs/widgets/common/dpopupmenu.cpp
blob: f41404ceca15ff972ae158ab8e8f1be3e24a0791 (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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/* ============================================================
 *
 * This file is a part of digiKam project
 * http://www.digikam.org
 *
 * Date        : 2006-11-11
 * Description : a popup menu with a decorative graphic banner
 *               at the left border.
 * 
 * Copyright (C) 1996-2000 the kicker authors.
 * Copyright (C) 2005 Mark Kretschmann <markey@web.de>
 * Copyright (C) 2006-2008 by Gilles Caulier <caulier dot gilles at gmail dot com>
 *
 * This program is free software; you can redistribute it
 * and/or modify it under the terms of the GNU General
 * Public License as published by the Free Software Foundation;
 * either version 2, or (at your option)
 * any later version.
 * 
 * This program 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 General Public License for more details.
 * 
 * ============================================================ */

// TQt includes.

#include <tqpainter.h>
#include <tqpixmap.h>
#include <tqstyle.h>

// KDE includes.

#include <tdeapplication.h>
#include <kiconeffect.h>
#include <tdeapplication.h>
#include <kstandarddirs.h>
#include <tdeaboutdata.h>

// Local includes.

#include "dpopupmenu.h"

namespace Digikam
{

static TQImage s_dpopupmenu_sidePixmap;
static TQColor s_dpopupmenu_sidePixmapColor;

DPopupMenu::DPopupMenu(TQWidget* parent, const char* name)
          : TDEPopupMenu(parent, name)
{
    // Must be initialized so that we know the size on first invocation
    if ( s_dpopupmenu_sidePixmap.isNull() )
        generateSidePixmap();
}

DPopupMenu::~DPopupMenu()
{
}

void DPopupMenu::generateSidePixmap()
{
    const TQColor newColor = calcPixmapColor();

    if ( newColor != s_dpopupmenu_sidePixmapColor ) 
    {
        s_dpopupmenu_sidePixmapColor = newColor;

        if (TDEApplication::kApplication()->aboutData()->appName() == TQString("digikam"))
            s_dpopupmenu_sidePixmap.load( locate( "data","digikam/data/menusidepixmap.png" ) );
        else
            s_dpopupmenu_sidePixmap.load( locate( "data","showfoto/menusidepixmap.png" ) );

        TDEIconEffect::colorize(s_dpopupmenu_sidePixmap, newColor, 1.0);
    }
}

int DPopupMenu::sidePixmapWidth() const
{
    return s_dpopupmenu_sidePixmap.width();
}

TQRect DPopupMenu::sideImageRect() const
{
    return TQStyle::visualRect(TQRect(frameWidth(), frameWidth(),
                                    s_dpopupmenu_sidePixmap.width(),
                                    height() - 2*frameWidth()),
                              this);
}

TQColor DPopupMenu::calcPixmapColor()
{
    TQColor color;
    TQColor activeTitle   = TQApplication::palette().active().background();
    TQColor inactiveTitle = TQApplication::palette().inactive().background();

    // figure out which color is most suitable for recoloring to
    int h1, s1, v1, h2, s2, v2, h3, s3, v3;
    activeTitle.hsv(&h1, &s1, &v1);
    inactiveTitle.hsv(&h2, &s2, &v2);
    TQApplication::palette().active().background().hsv(&h3, &s3, &v3);

    if ( (kAbs(h1-h3)+kAbs(s1-s3)+kAbs(v1-v3) < kAbs(h2-h3)+kAbs(s2-s3)+kAbs(v2-v3)) &&
            ((kAbs(h1-h3)+kAbs(s1-s3)+kAbs(v1-v3) < 32) || (s1 < 32)) && (s2 > s1))
        color = inactiveTitle;
    else
        color = activeTitle;

    // limit max/min brightness
    int r, g, b;
    color.rgb(&r, &g, &b);
    int gray = tqGray(r, g, b);
    if (gray > 180) 
    {
        r = (r - (gray - 180) < 0 ? 0 : r - (gray - 180));
        g = (g - (gray - 180) < 0 ? 0 : g - (gray - 180));
        b = (b - (gray - 180) < 0 ? 0 : b - (gray - 180));
    }
    else if (gray < 76) 
    {
        r = (r + (76 - gray) > 255 ? 255 : r + (76 - gray));
        g = (g + (76 - gray) > 255 ? 255 : g + (76 - gray));
        b = (b + (76 - gray) > 255 ? 255 : b + (76 - gray));
    }
    color.setRgb(r, g, b);

    return color;
}

void DPopupMenu::setMinimumSize(const TQSize & s)
{
    TDEPopupMenu::setMinimumSize(s.width() + s_dpopupmenu_sidePixmap.width(), s.height());
}

void DPopupMenu::setMaximumSize(const TQSize & s)
{
    TDEPopupMenu::setMaximumSize(s.width() + s_dpopupmenu_sidePixmap.width(), s.height());
}

void DPopupMenu::setMinimumSize(int w, int h)
{
    TDEPopupMenu::setMinimumSize(w + s_dpopupmenu_sidePixmap.width(), h);
}

void DPopupMenu::setMaximumSize(int w, int h)
{
  TDEPopupMenu::setMaximumSize(w + s_dpopupmenu_sidePixmap.width(), h);
}

void DPopupMenu::resizeEvent(TQResizeEvent * e)
{
    TDEPopupMenu::resizeEvent(e);

    setFrameRect(TQStyle::visualRect(TQRect(s_dpopupmenu_sidePixmap.width(), 0,
                                          width() - s_dpopupmenu_sidePixmap.width(), height()), 
                 this ) );
}

//Workaround TQt3.3.x sizing bug, by ensuring we're always wide enough.
void DPopupMenu::resize(int width, int height)
{
    width = kMax(width, maximumSize().width());
    TDEPopupMenu::resize(width, height);
}

void DPopupMenu::paintEvent(TQPaintEvent* e)
{
    generateSidePixmap();

    TQPainter p( this );

    TQRect r = sideImageRect();
    r.setTop(r.bottom()-s_dpopupmenu_sidePixmap.height()+1);
    if ( r.intersects( e->rect() ) )
    {
        TQRect drawRect = r.intersect(e->rect()).intersect(sideImageRect());
        TQRect pixRect  = drawRect;
        pixRect.moveBy(-r.left(), -r.top());
        p.drawImage(drawRect.topLeft(), s_dpopupmenu_sidePixmap, pixRect);
    }

    p.setClipRegion(e->region());

    //NOTE: The order is important here. drawContents() must be called before drawPrimitive(),
    //      otherwise we get rendering glitches.

    drawContents(&p);

    style().drawPrimitive(TQStyle::PE_PanelPopup, &p,
                          TQRect(0, 0, width(), height()),
                          colorGroup(), TQStyle::Style_Default,
                          TQStyleOption( frameWidth(), 0));
}

}  // namespace Digikam