TDE core libraries
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

statusbarextension.cpp 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /* This file is part of the KDE project
  2. Copyright (C) 2003 Daniel Molkentin <molkentin@kde.org>
  3. Copyright (C) 2003 David Faure <faure@kde.org>
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public
  6. License as published by the Free Software Foundation; either
  7. version 2 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Library General Public License for more details.
  12. You should have received a copy of the GNU Library General Public License
  13. along with this library; see the file COPYING.LIB. If not, write to
  14. the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  15. Boston, MA 02110-1301, USA.
  16. */
  17. #include "statusbarextension.h"
  18. #include <tqvaluelist.h>
  19. #include <tqobjectlist.h>
  20. #include <kstatusbar.h>
  21. #include <tdemainwindow.h>
  22. #include <kdebug.h>
  23. #include <tdelibs_export.h>
  24. #include <tdeparts/part.h>
  25. #include <tdeparts/event.h>
  26. using namespace KParts;
  27. ///////////////////////////////////////////////////////////////////
  28. // Helper Classes
  29. ///////////////////////////////////////////////////////////////////
  30. class KParts::StatusBarItem {
  31. public:
  32. StatusBarItem() // for QValueList
  33. : m_widget(0), m_visible(false)
  34. {}
  35. StatusBarItem( TQWidget * widget, int stretch, bool permanent )
  36. : m_widget(widget), m_stretch(stretch), m_permanent(permanent), m_visible(false)
  37. {}
  38. TQWidget * widget() const { return m_widget; }
  39. void ensureItemShown( KStatusBar * sb )
  40. {
  41. if ( !m_visible )
  42. {
  43. sb->addWidget( m_widget, m_stretch, m_permanent );
  44. m_visible = true;
  45. m_widget->show();
  46. }
  47. }
  48. void ensureItemHidden( KStatusBar * sb )
  49. {
  50. if ( m_visible )
  51. {
  52. sb->removeWidget( m_widget );
  53. m_visible = false;
  54. m_widget->hide();
  55. }
  56. }
  57. private:
  58. TQWidget * m_widget;
  59. int m_stretch;
  60. bool m_permanent;
  61. bool m_visible; // true when the item has been added to the statusbar
  62. };
  63. ///////////////////////////////////////////////////////////////////
  64. StatusBarExtension::StatusBarExtension(KParts::ReadOnlyPart *parent, const char* name)
  65. : TQObject(parent, name), m_statusBar(0), d(0)
  66. {
  67. parent->installEventFilter(this);
  68. }
  69. StatusBarExtension::~StatusBarExtension()
  70. {
  71. }
  72. StatusBarExtension *StatusBarExtension::childObject( TQObject *obj )
  73. {
  74. if ( !obj || obj->childrenListObject().isEmpty() )
  75. return 0L;
  76. // we try to do it on our own, in hope that we are faster than
  77. // queryList, which looks kind of big :-)
  78. const TQObjectList children = obj->childrenListObject();
  79. TQObjectListIt it( children );
  80. for (; it.current(); ++it )
  81. if ( it.current()->inherits( "KParts::StatusBarExtension" ) )
  82. return static_cast<KParts::StatusBarExtension *>( it.current() );
  83. return 0L;
  84. }
  85. bool StatusBarExtension::eventFilter(TQObject * watched, TQEvent* ev)
  86. {
  87. if ( !GUIActivateEvent::test( ev ) ||
  88. !watched->inherits("KParts::ReadOnlyPart") )
  89. return TQObject::eventFilter(watched, ev);
  90. KStatusBar * sb = statusBar();
  91. if ( !sb )
  92. return TQObject::eventFilter(watched, ev);
  93. GUIActivateEvent *gae = static_cast<GUIActivateEvent*>(ev);
  94. if ( gae->activated() )
  95. {
  96. TQValueListIterator<StatusBarItem> it = m_statusBarItems.begin();
  97. for ( ; it != m_statusBarItems.end() ; ++it )
  98. (*it).ensureItemShown( sb );
  99. }
  100. else
  101. {
  102. TQValueListIterator<StatusBarItem> it = m_statusBarItems.begin();
  103. for ( ; it != m_statusBarItems.end() ; ++it )
  104. (*it).ensureItemHidden( sb );
  105. }
  106. return false;
  107. }
  108. KStatusBar * StatusBarExtension::statusBar() const
  109. {
  110. if ( !m_statusBar ) {
  111. TQWidget* w = static_cast<KParts::ReadOnlyPart*>(parent())->widget();
  112. TDEMainWindow* mw = tqt_dynamic_cast<TDEMainWindow *>( w->topLevelWidget() );
  113. if ( mw )
  114. m_statusBar = mw->statusBar();
  115. }
  116. return m_statusBar;
  117. }
  118. void StatusBarExtension::setStatusBar( KStatusBar* status )
  119. {
  120. m_statusBar = status;
  121. }
  122. void StatusBarExtension::addStatusBarItem( TQWidget * widget, int stretch, bool permanent )
  123. {
  124. m_statusBarItems.append( StatusBarItem( widget, stretch, permanent ) );
  125. TQValueListIterator<StatusBarItem> it = m_statusBarItems.fromLast();
  126. KStatusBar * sb = statusBar();
  127. Q_ASSERT(sb);
  128. if (sb)
  129. (*it).ensureItemShown( sb );
  130. }
  131. void StatusBarExtension::removeStatusBarItem( TQWidget * widget )
  132. {
  133. KStatusBar * sb = statusBar();
  134. TQValueListIterator<StatusBarItem> it = m_statusBarItems.begin();
  135. for ( ; it != m_statusBarItems.end() ; ++it )
  136. if ( (*it).widget() == widget )
  137. {
  138. if ( sb )
  139. (*it).ensureItemHidden( sb );
  140. m_statusBarItems.remove( it );
  141. break;
  142. }
  143. if ( it == m_statusBarItems.end() )
  144. kdWarning(1000) << "StatusBarExtension::removeStatusBarItem. Widget not found : " << widget << endl;
  145. }
  146. #include "statusbarextension.moc"
  147. // vim: ts=2 sw=2 et