1
// -*- Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 8; -*-
2
/* This file is part of the KDE project
3
Copyright (C) 2004 Esben Mose Hansen <kde@mosehansen.dk>
5
This program is free software; you can redistribute it and/or
6
modify it under the terms of the GNU General Public
7
License as published by the Free Software Foundation; either
8
version 2 of the License, or (at your option) any later version.
10
This program is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
General Public License for more details.
15
You should have received a copy of the GNU General Public License
16
along with this program; see the file COPYING. If not, write to
17
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18
Boston, MA 02110-1301, USA.
24
#include <QStyleOption>
26
#include <kstringhandler.h>
30
#include "historyitem.h"
31
#include "popupproxy.h"
33
#include "klipperpopup.h"
36
PopupProxy::PopupProxy( KlipperPopup* parent, int menu_height, int menu_width )
38
m_proxy_for_menu( parent ),
40
m_menu_height( menu_height ),
41
m_menu_width( menu_width )
43
if (!parent->history()->empty()) {
44
m_spill_uuid = parent->history()->first()->uuid();
46
connect( parent->history(), SIGNAL( changed() ), SLOT( slotHistoryChanged() ) );
47
connect(m_proxy_for_menu, SIGNAL(triggered(QAction*)), parent->history(), SLOT(slotMoveToTop(QAction*)));
50
void PopupProxy::slotHistoryChanged() {
55
void PopupProxy::deleteMoreMenus() {
56
const KMenu* myParent = parent();
57
if ( myParent != m_proxy_for_menu ) {
58
KMenu* delme = m_proxy_for_menu;
59
m_proxy_for_menu = static_cast<KMenu*>( m_proxy_for_menu->parent() );
60
while ( m_proxy_for_menu != myParent ) {
61
delme = m_proxy_for_menu;
62
m_proxy_for_menu = static_cast<KMenu*>( m_proxy_for_menu->parent() );
64
// We are called probably from within the menus event-handler (triggered=>slotMoveToTop=>changed=>slotHistoryChanged=>deleteMoreMenus)
65
// what can result in a crash if we just delete the menu here (#155196 and #165154) So, delay the delete.
70
int PopupProxy::buildParent( int index, const QRegExp& filter ) {
72
// Start from top of history (again)
73
m_spill_uuid = parent()->history()->empty() ? QByteArray() : parent()->history()->first()->uuid();
74
if ( filter.isValid() ) {
78
return insertFromSpill( index );
82
KlipperPopup* PopupProxy::parent() {
83
return static_cast<KlipperPopup*>( QObject::parent() );
86
void PopupProxy::slotAboutToShow() {
90
void PopupProxy::tryInsertItem( HistoryItem const * const item,
94
QAction *action = new QAction(m_proxy_for_menu);
95
QPixmap image( item->image() );
96
if ( image.isNull() ) {
97
// Squeeze text strings so that do not take up the entire screen (or more)
98
QString text = m_proxy_for_menu->fontMetrics().elidedText( item->text().simplified(), Qt::ElideMiddle, m_menu_width );
99
text.replace( '&', "&&" );
100
action->setText(text);
102
#if 0 // not used because QAction#setIcon does not respect this size; it does scale anyway. TODO: find a way to set a bigger image
103
const QSize max_size( m_menu_width,m_menu_height/4 );
104
if ( image.height() > max_size.height() || image.width() > max_size.width() ) {
105
image = image.scaled( max_size, Qt::KeepAspectRatio, Qt::SmoothTransformation );
108
action->setIcon(QIcon(image));
111
action->setData(item->uuid());
113
// if the m_proxy_for_menu is a submenu (aka a "More" menu) then it may the case, that there is no other action in that menu yet.
114
QAction *before = index < m_proxy_for_menu->actions().count() ? m_proxy_for_menu->actions().at(index) : 0;
115
// insert the new action to the m_proxy_for_menu
116
m_proxy_for_menu->insertAction(before, action);
118
// Determine height of a menu item.
119
QStyleOptionMenuItem style_options;
120
// It would be much easier to use QMenu::initStyleOptions. But that is protected, so until we have a better
121
// excuse to subclass that, I'd rather implement this manually.
122
// Note 2 properties, tabwidth and maxIconWidth, are not available from the public interface, so those are left out (probably not
123
// important for height. Also, Exlsive checkType is disregarded as I don't think we will ever use it)
124
style_options.initFrom(m_proxy_for_menu);
125
style_options.checkType = action->isCheckable() ? QStyleOptionMenuItem::NonExclusive : QStyleOptionMenuItem::NotCheckable;
126
style_options.checked = action->isChecked();
127
style_options.font = action->font();
128
style_options.icon = action->icon();
129
style_options.menuHasCheckableItems = true;
130
style_options.menuRect = m_proxy_for_menu->rect();
131
style_options.text = action->text();
133
int font_height = QFontMetrics(m_proxy_for_menu->fontMetrics()).height();
135
int itemheight = m_proxy_for_menu->style()->sizeFromContents(QStyle::CT_MenuItem,
137
QSize( 0, font_height ),
138
m_proxy_for_menu).height();
139
// Subtract the used height
140
remainingHeight -= itemheight;
143
int PopupProxy::insertFromSpill( int index ) {
145
const History* history = parent()->history();
146
// This menu is going to be filled, so we don't need the aboutToShow()
148
disconnect( m_proxy_for_menu, 0, this, 0 );
150
// Insert history items into the current m_proxy_for_menu,
151
// discarding any that doesn't match the current filter.
152
// stop when the total number of items equal m_itemsPerMenu;
154
int remainingHeight = m_menu_height - m_proxy_for_menu->sizeHint().height();
155
const HistoryItem* item = history->find(m_spill_uuid);
160
if ( m_filter.indexIn( item->text() ) != -1) {
161
tryInsertItem( item, remainingHeight, index++ );
164
item = history->find(item->next_uuid());
165
} while ( item && history->first() != item && remainingHeight >= 0);
166
m_spill_uuid = item->uuid();
168
// If there is more items in the history, insert a new "More..." menu and
169
// make *this a proxy for that menu ('s content).
170
if (history->first() && m_spill_uuid != history->first()->uuid()) {
171
KMenu* moreMenu = new KMenu(i18n("&More"), m_proxy_for_menu);
172
connect(moreMenu, SIGNAL(aboutToShow()), SLOT(slotAboutToShow()));
173
QAction *before = index < m_proxy_for_menu->actions().count() ? m_proxy_for_menu->actions().at(index) : 0;
174
m_proxy_for_menu->insertMenu(before, moreMenu);
175
m_proxy_for_menu = moreMenu;
178
// Return the number of items inserted.
182
#include "popupproxy.moc"