/***************************************************************************
 *   Copyright (C) 2004 by Hans Oischinger                                 *
 *   hans.oischinger@kdemail.net                                           *
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 *   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.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Steet, Fifth Floor, Boston, MA 02110-1301, USA.             *
 ***************************************************************************/
#ifndef STOPSEDITORWIDGET_H
#define STOPSEDITORWIDGET_H

#include <qwidget.h>
#include <qptrdict.h>

class Gradient;
class StopElement;
class StopLabel;
/**
A widget that provides editing functionality for Gradient stops
 
@author Hans Oischinger
*/
class StopsEditorWidget : public QWidget
{
  Q_OBJECT
public:
  StopsEditorWidget(QWidget *parent = 0, const char *name = 0);
  ~StopsEditorWidget();

  /**
   * Set the gradient to operate on (add and remove child StopElements)
   * Be aware that this StopsEditorWidget will operate on the reference and not on a copy
   */
  void setGradient( Gradient* );

  StopElement* getSelectedStop() { return m_selStop; }

public slots:
  void slotStopModifiedFromOutside(StopElement*);

signals:
  void stopsModified(StopElement*);
  void selectionChanged();
  /**
   * Issued when a StopElement has been selected.
   * @param will be 0 when no StopElement has been selected
   */
  void stopSelected( StopElement* );

protected:
  /**
   * Creates a new StopLabel for the given StopElement
   * @param select set to true if you want to select it immediately
   */
  void createStopLabel( StopElement*, bool select = false );

  void resizeEvent(QResizeEvent *);
  void mousePressEvent ( QMouseEvent * e );
  void mouseReleaseEvent ( QMouseEvent * e );

protected slots:
  void slotStopSelectionChanged(StopLabel*);
  void slotStopModified(StopLabel*);

private:
  Gradient* m_gradient;
  QPtrDict<StopLabel> m_stopsToLabels;
  StopElement* m_selStop;
  QPoint m_mousePos;
};


#include <qlabel.h>
class StopLabel : public QLabel
{
  Q_OBJECT
public:
  StopLabel(StopElement* element, StopsEditorWidget *parent);
  ~StopLabel() {}

  StopElement* getStop() const { return m_stop; }

  /**
   * Applies the color and position of the stop element
   */
  void syncToStop();

  /**
   * Call to layout acording to parent size()
   */
  void reLayout();

signals:
  void stopModified(StopLabel*);
  void selected( StopLabel* );

protected:
  /**
   * Updates the stop's offset to the current x position
   */
  void updateStopToPosition();

  void mousePressEvent ( QMouseEvent * e );
  void mouseReleaseEvent ( QMouseEvent * e );
  void mouseMoveEvent ( QMouseEvent * e );

protected slots:
  void slotSelectionChanged( StopElement* );

protected:
  StopsEditorWidget *m_parentWidget;
  StopElement* m_stop;
  bool m_dragactive;
  int m_xoffset;
  QPoint m_mousePos;
};

#endif
