/***************************************************************************
                         kbandwidthview.h  -  description
                             -------------------
    copyright            : (C) 2007 by Hans Dembinski
    email                : kbandwidth@5th-heaven.org
    original author      : Niko Sommer
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#ifndef APPLETVIEW
#define APPLETVIEW

#include <qwidget.h>
#include <qtooltip.h>
#include <qmap.h>
#include <qpixmap.h>
#include <qdatetime.h>

class KConfig;
class QPaintEvent;
class QTimerEvent;
class QPoint;
class QColor;
class PreferencesDialog;
class QString;
class QStringList;

struct Bandwidth {
  unsigned long incoming;
  unsigned long outgoing;
  QString interface;

  Bandwidth() :
    incoming(0),
    outgoing(0),
    interface()
  {}
};

class StateData : public QMap<QString, Bandwidth> {
public:
  QTime time;

  StateData() :
    QMap<QString, Bandwidth>(),
    time()
  {}

  StateData& operator=(const StateData& source) {
    this->QMap<QString, Bandwidth>::operator=(source);
    time = source.time;
    return *this;
  }
};

class KBandwidthView : public QWidget, QToolTip
{
  Q_OBJECT

friend class PreferencesDialog;

public:
  KConfig* kConfig;

  KBandwidthView( KConfig* ksCfg, QWidget* parent = 0, const char* name = 0, WFlags fl = 0 );
  ~KBandwidthView();

  int setHorizontal(const int& height);
  int setVertical  (const int& width );

signals:
  void configChanged();

protected:
  virtual void maybeTip(const QPoint&);
  virtual void paintEvent( QPaintEvent * );
  virtual void timerEvent( QTimerEvent * );
  virtual void writeConfig();
  virtual void updateConfig();

protected slots:
  virtual void languageChange();

private:
  StateData previous;
  Bandwidth bandwidth;

  void readInterfaceData (StateData& state);
  QStringList listInterfaces();
  void calculateBandwidth();
  void generateArrowPixmaps();

  int       timerId;

  QPixmap   arrowIncoming;
  QPixmap   arrowOutgoing;
  QPixmap   arrowIncomingOn;
  QPixmap   arrowOutgoingOn;

  enum Orientation { eHorizontal, eVertical };
  Orientation orientation;

  // config values
  QMap<QString,Bandwidth> limits;

  QString   forceInterface;
  ulong     divisor;
  int       updateInterval;

  QColor    colorNormalIncoming;
  QColor    colorNormalOutgoing;
  QColor    colorArrowIncoming;
  QColor    colorArrowOutgoing;

  int       arrowTransparency;

  QColor    colorText;

  bool      showText;
  bool      showArrows;
  bool      flagLearn;

  int       arrowWidth;
  int       textWidth;
};

#endif
