[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-core-devel
Subject:    KToolBarLabelAction class
From:       Felix Berger <bflat1 () gmx ! net>
Date:       2004-06-30 15:50:47
Message-ID: 200406301750.47794.bflat1 () gmx ! net
[Download RAW message or body]

Hi,

The attached files provide a convenience class for displaying labels in 
toolbars.

I've noticed that most projects have reimplemented this functionality again 
and again, mostly by copying from "konq_misc.cc"

The code could also be moved to "kactionclasses.[cpp/h]" if that's 
preferrable.

Please review.

Sincerely,
Felix Berger

-- 
Use Debian GNU/Linux!
http://www.felix.beldesign.de/

["ktoolbarlabelaction.cpp" (text/x-c++src)]

/* This file is part of the KDE libraries
    Copyright (C) 2004 Felix Berger <felixberger@beldesign.de>
    
    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License version 2 as published by the Free Software Foundation.

    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
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    Boston, MA 02111-1307, USA.
*/

#include "ktoolbarlabelaction.h"

#include <qlabel.h>
#include <qapplication.h>

KToolBarLabelAction::KToolBarLabelAction(const QString &text,
					 const KShortcut &cut,
					 const QObject *receiver, 
					 const char *slot,
					 KActionCollection *parent,
					 const char *name)
  : KWidgetAction(new QLabel(text, 0, "kde toolbar widget"), text, cut,
		  receiver, slot, parent, name)
{
  init();
}

KToolBarLabelAction::KToolBarLabelAction(QWidget* buddy, 
					 const QString &text,
					 const KShortcut &cut,
					 const QObject *receiver, 
					 const char *slot,
					 KActionCollection *parent, 
					 const char *name)
  : KWidgetAction(new QLabel(buddy, text, 0, "kde toolbar widget"), text, 
		  cut, receiver, slot, parent, name)
{
  init();
}

KToolBarLabelAction::~KToolBarLabelAction()
{
  delete m_label;
  m_label = 0;
}

void KToolBarLabelAction::init()
{
  m_label = static_cast<QLabel*>(widget());
  m_label->setBackgroundMode(Qt::PaletteButton);
  m_label->setAlignment((QApplication::reverseLayout()
		       ? Qt::AlignRight : Qt::AlignLeft) |
	      Qt::AlignVCenter | Qt::ShowPrefix );
  m_label->adjustSize();
}

void KToolBarLabelAction::setText(const QString& text)
{
  KWidgetAction::setText(text);
  m_label->setText(text);
}


void KToolBarLabelAction::setBuddy(QWidget* buddy)
{
  m_label->setBuddy(buddy);
}


QWidget* KToolBarLabelAction::buddy() const
{
  return m_label->buddy();
}

QLabel* KToolBarLabelAction::label() const
{
  return m_label;
}

["ktoolbarlabelaction.h" (text/x-chdr)]

/* This file is part of the KDE libraries
    Copyright (C) 2004 Felix Berger <felixberger@beldesign.de>
    
    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License version 2 as published by the Free Software Foundation.

    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
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    Boston, MA 02111-1307, USA.
*/
#ifndef KTOOLBARLABELACTION_H
#define KTOOLBARLABELACTION_H

#include "kactionclasses.h"

class QLabel;

/**
 * @short Class to display a label in a toolbar.
 *
 * KToolBarLabelAction is a convenience class for displaying a label in a
 * toolbar. 
 *
 * It provides easy access to the label's #setBuddy(QWidget*) and #buddy()
 * methods and can be used as follows:
 *
 * \code
 *
 * KHistoryCombo* findCombo = new KHistoryCombo(true, this);
 * KWidgetAction* action
 *   = new KWidgetAction(findCombo, i18n("F&ind Combo"), Qt::Key_F6, this,
 *                       SLOT(slotFocus()), actionCollection(), "find_combo");
 *
 * new KToolBarLabelAction(findCombo, i18n("F&ind "), 0, this, 
 *                         SLOT(slotFocus()), actionCollection(), 
 *			   "find_label");
 *
 * \endcode
 *
 * @author Felix Berger <felixberger@beldesign.de>
 */
class KToolBarLabelAction : public KWidgetAction
{
public:
  /**
   * Constructs a tool bar label.
   *
   * @param text The label's and the action's text.
   * @param cut The action's shortcut.
   * @param receiver The SLOT's parent.
   * @param slot The SLOT to invoke to execute this action.
   * @param parent This action's parent.
   * @param name An internal name for this action.
   */
  KToolBarLabelAction(const QString &text, 
		      const KShortcut &cut,
		      const QObject *receiver, const char *slot,
		      KActionCollection *parent, const char *name);
  /**
   * Constructs a tool bar label setting a buddy for the label.
   *
   * @param buddy The widget which is focused when the label's accelerator is
   * typed.
   * @param text The label's and the action's text.
   * @param cut The action's shortcut.
   * @param receiver The SLOT's parent.
   * @param slot The SLOT to invoke to execute this action.
   * @param parent This action's parent.
   * @param name An internal name for this action.
   */
  KToolBarLabelAction(QWidget* buddy, const QString &text,
		      const KShortcut &cut,
		      const QObject *receiver, const char *slot,
		      KActionCollection *parent, const char *name);
  virtual ~KToolBarLabelAction();
  /**
   * Reimplemented to update both the action's text and the label's text.
   */
  virtual void setText(const QString& text);
  /**
   * Sets the label's buddy to buddy.
   *
   * See QLabel#setBuddy() for details.
   */
  virtual void setBuddy(QWidget* buddy);
  /**
   * Returns the label's buddy or 0 if no buddy is currently set.
   *
   * See QLabel#buddy() and QLabel#setBuddy() for more information.
   */
  QWidget* buddy() const;
  /**
   * Returns the label used internally.
   */
  QLabel* label() const;

private:
  QLabel* m_label;
  void init();
};


#endif


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic