📄 qwt_legend.cpp
字号:
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
* Qwt Widget Library
* Copyright (C) 1997 Josef Wilgen
* Copyright (C) 2002 Uwe Rathmann
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the Qwt License, Version 1.0
*****************************************************************************/
// vim: expandtab
#include <qapplication.h>
#include <qmap.h>
#if QT_VERSION >= 0x040000
#include <qscrollbar.h>
#endif
#include "qwt_math.h"
#include "qwt_dyngrid_layout.h"
#include "qwt_plot_item.h"
#include "qwt_legend_item.h"
#include "qwt_legend.h"
class QwtLegend::PrivateData
{
public:
class LegendMap
{
public:
void insert(const QwtPlotItem *, QWidget *);
void remove(const QwtPlotItem *);
void remove(QWidget *);
void clear();
uint count() const;
inline const QWidget *find(const QwtPlotItem *) const;
inline QWidget *find(const QwtPlotItem *);
inline const QwtPlotItem *find(const QWidget *) const;
inline QwtPlotItem *find(const QWidget *);
const QMap<QWidget *, const QwtPlotItem *> &widgetMap() const;
QMap<QWidget *, const QwtPlotItem *> &widgetMap();
private:
QMap<QWidget *, const QwtPlotItem *> d_widgetMap;
QMap<const QwtPlotItem *, QWidget *> d_itemMap;
};
QwtLegend::LegendItemMode itemMode;
QwtLegend::LegendDisplayPolicy displayPolicy;
int identifierMode;
LegendMap map;
class LegendView;
LegendView *view;
};
#if QT_VERSION < 0x040000
#include <qscrollview.h>
class QwtLegend::PrivateData::LegendView: public QScrollView
{
public:
LegendView(QWidget *parent):
QScrollView(parent)
{
setResizePolicy(Manual);
viewport()->setBackgroundMode(Qt::NoBackground); // Avoid flicker
contentsWidget = new QWidget(viewport());
addChild(contentsWidget);
}
void viewportResizeEvent(QResizeEvent *e)
{
QScrollView::viewportResizeEvent(e);
// It's not safe to update the layout now, because
// we are in an internal update of the scrollview framework.
// So we delay the update by posting a LayoutHint.
QApplication::postEvent(contentsWidget,
new QEvent(QEvent::LayoutHint));
}
QWidget *contentsWidget;
};
#else // QT_VERSION >= 0x040000
#include <qscrollarea.h>
class QwtLegend::PrivateData::LegendView: public QScrollArea
{
public:
LegendView(QWidget *parent):
QScrollArea(parent)
{
contentsWidget = new QWidget(this);
setWidget(contentsWidget);
setWidgetResizable(false);
setFocusPolicy(Qt::NoFocus);
}
virtual bool viewportEvent(QEvent *e)
{
bool ok = QScrollArea::viewportEvent(e);
if ( e->type() == QEvent::Resize )
{
QApplication::sendEvent(contentsWidget,
new QEvent(QEvent::LayoutRequest));
}
return ok;
}
QSize viewportSize(int w, int h) const
{
const int sbHeight = horizontalScrollBar()->sizeHint().height();
const int sbWidth = verticalScrollBar()->sizeHint().width();
const int cw = contentsRect().width();
const int ch = contentsRect().height();
int vw = cw;
int vh = ch;
if ( w > vw )
vh -= sbHeight;
if ( h > vh )
{
vw -= sbWidth;
if ( w > vw && vh == ch )
vh -= sbHeight;
}
return QSize(vw, vh);
}
QWidget *contentsWidget;
};
#endif
void QwtLegend::PrivateData::LegendMap::insert(
const QwtPlotItem *item, QWidget *widget)
{
d_itemMap.insert(item, widget);
d_widgetMap.insert(widget, item);
}
void QwtLegend::PrivateData::LegendMap::remove(const QwtPlotItem *item)
{
QWidget *widget = d_itemMap[item];
d_itemMap.remove(item);
d_widgetMap.remove(widget);
}
void QwtLegend::PrivateData::LegendMap::remove(QWidget *widget)
{
const QwtPlotItem *item = d_widgetMap[widget];
d_itemMap.remove(item);
d_widgetMap.remove(widget);
}
void QwtLegend::PrivateData::LegendMap::clear()
{
/*
We can't delete the widgets in the following loop, because
we would get ChildRemoved events, changing d_itemMap, while
we are iterating.
*/
#if QT_VERSION < 0x040000
QValueList<QWidget *> widgets;
QMap<const QwtPlotItem *, QWidget *>::const_iterator it;
for ( it = d_itemMap.begin(); it != d_itemMap.end(); ++it )
widgets.append(it.data());
#else
QList<QWidget *> widgets;
QMap<const QwtPlotItem *, QWidget *>::const_iterator it;
for ( it = d_itemMap.begin(); it != d_itemMap.end(); ++it )
widgets.append(it.value());
#endif
d_itemMap.clear();
d_widgetMap.clear();
for ( int i = 0; i < (int)widgets.size(); i++ )
delete widgets[i];
}
uint QwtLegend::PrivateData::LegendMap::count() const
{
return d_itemMap.count();
}
inline const QWidget *QwtLegend::PrivateData::LegendMap::find(const QwtPlotItem *item) const
{
if ( !d_itemMap.contains((QwtPlotItem *)item) )
return NULL;
return d_itemMap[(QwtPlotItem *)item];
}
inline QWidget *QwtLegend::PrivateData::LegendMap::find(const QwtPlotItem *item)
{
if ( !d_itemMap.contains((QwtPlotItem *)item) )
return NULL;
return d_itemMap[(QwtPlotItem *)item];
}
inline const QwtPlotItem *QwtLegend::PrivateData::LegendMap::find(
const QWidget *widget) const
{
if ( !d_widgetMap.contains((QWidget *)widget) )
return NULL;
return d_widgetMap[(QWidget *)widget];
}
inline QwtPlotItem *QwtLegend::PrivateData::LegendMap::find(
const QWidget *widget)
{
if ( !d_widgetMap.contains((QWidget *)widget) )
return NULL;
return (QwtPlotItem *)d_widgetMap[(QWidget *)widget];
}
inline const QMap<QWidget *, const QwtPlotItem *> &
QwtLegend::PrivateData::LegendMap::widgetMap() const
{
return d_widgetMap;
}
inline QMap<QWidget *, const QwtPlotItem *> &
QwtLegend::PrivateData::LegendMap::widgetMap()
{
return d_widgetMap;
}
/*!
\param parent Parent widget
*/
QwtLegend::QwtLegend(QWidget *parent):
QFrame(parent)
{
setFrameStyle(NoFrame);
d_data = new QwtLegend::PrivateData;
d_data->itemMode = QwtLegend::ReadOnlyItem;
d_data->displayPolicy = QwtLegend::AutoIdentifier;
d_data->identifierMode = QwtLegendItem::ShowLine |
QwtLegendItem::ShowSymbol | QwtLegendItem::ShowText;
d_data->view = new QwtLegend::PrivateData::LegendView(this);
d_data->view->setFrameStyle(NoFrame);
QwtDynGridLayout *layout = new QwtDynGridLayout(
d_data->view->contentsWidget);
#if QT_VERSION < 0x040000
layout->setAutoAdd(true);
#endif
layout->setAlignment(Qt::AlignHCenter | Qt::AlignTop);
d_data->view->contentsWidget->installEventFilter(this);
}
//! Destructor
QwtLegend::~QwtLegend()
{
delete d_data;
}
/*!
Set the legend display policy to:
\param policy Legend display policy
\param mode Identifier mode (or'd ShowLine, ShowSymbol, ShowText)
\sa displayPolicy, LegendDisplayPolicy
*/
void QwtLegend::setDisplayPolicy(LegendDisplayPolicy policy, int mode)
{
d_data->displayPolicy = policy;
if (-1 != mode)
d_data->identifierMode = mode;
QMap<QWidget *, const QwtPlotItem *> &map =
d_data->map.widgetMap();
QMap<QWidget *, const QwtPlotItem *>::iterator it;
for ( it = map.begin(); it != map.end(); ++it )
{
#if QT_VERSION < 0x040000
QwtPlotItem *item = (QwtPlotItem *)it.data();
#else
QwtPlotItem *item = (QwtPlotItem *)it.value();
#endif
if ( item )
item->updateLegend(this);
}
}
/*!
\return the legend display policy.
Default is LegendDisplayPolicy::Auto.
\sa setDisplayPolicy, LegendDisplayPolicy
*/
QwtLegend::LegendDisplayPolicy QwtLegend::displayPolicy() const
{
return d_data->displayPolicy;
}
void QwtLegend::setItemMode(LegendItemMode mode)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -