summaryrefslogtreecommitdiffstats
path: root/chalk/ui/squeezedcombobox.cpp
diff options
context:
space:
mode:
authortpearson <tpearson@283d02a7-25f6-0310-bc7c-ecb5cbfe19da>2011-06-26 00:41:16 +0000
committertpearson <tpearson@283d02a7-25f6-0310-bc7c-ecb5cbfe19da>2011-06-26 00:41:16 +0000
commit698569f8428ca088f764d704034a1330517b98c0 (patch)
treebf45be6946ebbbee9cce5a5bcf838f4c952d87e6 /chalk/ui/squeezedcombobox.cpp
parent2785103a6bd4de55bd26d79e34d0fdd4b329a73a (diff)
downloadkoffice-698569f8428ca088f764d704034a1330517b98c0.tar.gz
koffice-698569f8428ca088f764d704034a1330517b98c0.zip
Finish rebranding of Krita as Chalk
git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/applications/koffice@1238363 283d02a7-25f6-0310-bc7c-ecb5cbfe19da
Diffstat (limited to 'chalk/ui/squeezedcombobox.cpp')
-rw-r--r--chalk/ui/squeezedcombobox.cpp167
1 files changed, 167 insertions, 0 deletions
diff --git a/chalk/ui/squeezedcombobox.cpp b/chalk/ui/squeezedcombobox.cpp
new file mode 100644
index 00000000..96929450
--- /dev/null
+++ b/chalk/ui/squeezedcombobox.cpp
@@ -0,0 +1,167 @@
+/* ============================================================
+ * Author: Tom Albers <[email protected]>
+ * Date : 2005-01-01
+ * Description :
+ *
+ * Copyright 2005 by Tom Albers
+ *
+ * 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.
+ *
+ * ============================================================ */
+
+/** @file squeezedcombobox.cpp */
+
+// TQt includes.
+
+#include <tqlistbox.h>
+#include <tqcombobox.h>
+#include <tqpair.h>
+#include <tqtimer.h>
+#include <tqvaluelist.h>
+#include <tqstyle.h>
+#include <tqapplication.h>
+#include <tqtooltip.h>
+
+// Local includes.
+
+#include "squeezedcombobox.h"
+
+SqueezedComboBoxTip::SqueezedComboBoxTip( TQWidget * tqparent, SqueezedComboBox* name )
+ : TQToolTip( tqparent )
+{
+ m_originalWidget = name;
+}
+
+void SqueezedComboBoxTip::maybeTip( const TQPoint &pos )
+{
+ TQListBox* listBox = m_originalWidget->listBox();
+ if (!listBox)
+ return;
+
+ TQListBoxItem* selectedItem = listBox->itemAt( pos );
+ if (selectedItem)
+ {
+ TQRect positionToolTip = listBox->tqitemRect( selectedItem );
+ TQString toolTipText = m_originalWidget->itemHighlighted();
+ if (!toolTipText.isNull())
+ tip(positionToolTip, toolTipText);
+ }
+}
+
+SqueezedComboBox::SqueezedComboBox( TQWidget *tqparent, const char *name )
+ : TQComboBox( tqparent, name )
+{
+ setMinimumWidth(100);
+ m_timer = new TQTimer(this);
+ m_tooltip = new SqueezedComboBoxTip( listBox()->viewport(), this );
+
+ connect(m_timer, TQT_SIGNAL(timeout()),
+ TQT_SLOT(slotTimeOut()));
+ connect(this, TQT_SIGNAL(activated( int )),
+ TQT_SLOT(slotUpdateToolTip( int )));
+}
+
+SqueezedComboBox::~SqueezedComboBox()
+{
+ delete m_tooltip;
+ delete m_timer;
+}
+
+bool SqueezedComboBox::tqcontains( const TQString& _text ) const
+{
+ if ( _text.isEmpty() )
+ return false;
+
+ const int itemCount = count();
+ for (int i = 0; i < itemCount; ++i )
+ {
+ if ( text(i) == _text )
+ return true;
+ }
+ return false;
+}
+
+TQSize SqueezedComboBox::tqsizeHint() const
+{
+ constPolish();
+ TQFontMetrics fm = fontMetrics();
+
+ int maxW = count() ? 18 : 7 * fm.width(TQChar('x')) + 18;
+ int maxH = TQMAX( fm.lineSpacing(), 14 ) + 2;
+
+ return tqstyle().tqsizeFromContents(TQStyle::CT_ComboBox, this,
+ TQSize(maxW, maxH)).
+ expandedTo(TQApplication::globalStrut());
+}
+
+void SqueezedComboBox::insertSqueezedItem(const TQString& newItem, int index)
+{
+ m_originalItems[index] = newItem;
+ insertItem( squeezeText(newItem), index );
+
+ // if this is the first item, set the tooltip.
+ if (index == 0)
+ slotUpdateToolTip(0);
+}
+
+void SqueezedComboBox::resizeEvent ( TQResizeEvent * )
+{
+ m_timer->start(200, true);
+}
+
+void SqueezedComboBox::slotTimeOut()
+{
+ TQMapIterator<int,TQString> it;
+ for (it = m_originalItems.begin() ; it != m_originalItems.end();
+ ++it)
+ {
+ changeItem( squeezeText( it.data() ), it.key() );
+ }
+}
+
+TQString SqueezedComboBox::squeezeText( const TQString& original)
+{
+ // not the complete widgetSize is usable. Need to compensate for that.
+ int widgetSize = width()-30;
+ TQFontMetrics fm( fontMetrics() );
+
+ // If we can fit the full text, return that.
+ if (fm.width(original) < widgetSize)
+ return(original);
+
+ // We need to squeeze.
+ TQString sqItem = original; // prevent empty return value;
+ widgetSize = widgetSize-fm.width("...");
+ for (uint i = 0 ; i != original.length(); ++i)
+ {
+ if ( (int)fm.width(original.right(i)) > widgetSize)
+ {
+ sqItem = TQString("..." + original.right(--i));
+ break;
+ }
+ }
+ return sqItem;
+}
+
+void SqueezedComboBox::slotUpdateToolTip( int index )
+{
+ TQToolTip::remove(this);
+ TQToolTip::add(this, m_originalItems[index]);
+}
+
+TQString SqueezedComboBox::itemHighlighted()
+{
+ int curItem = this->listBox()->currentItem();
+ return m_originalItems[curItem];
+}
+
+#include "squeezedcombobox.moc"