// $Id: lprofmain.h,v 1.52 2008/03/06 21:18:59 hvengel Exp $
//  Little cms - profiler construction set
//  Copyright (C) 1998-2001 Marti Maria
//  Copyright (C) 2005 Hal Engel
//
// THIS SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
// EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
// WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
//
// IN NO EVENT SHALL THE DEVELOPERS BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
// INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
// OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
// WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
// LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
// OF THIS SOFTWARE.
//
// 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 3 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, see <http://www.gnu.org/licenses/>.
//
// Version 1.11

#ifndef LPROFMAIN_H
#define LPROFMAIN_H

#include "lcmsprf.h"
#include <q3canvas.h>
#include <qimage.h>
#include <qassistantclient.h>
#include <qsettings.h>
#include <qmutex.h>
#include <q3urloperator.h>
#include <QDialog>
//Added by qt3to4:
#include <QTranslator>
#include <QPixmap>
#include <QResizeEvent>
#include <QMouseEvent>
#include <Q3VBoxLayout>

#include "vigra/config.hxx"
#include "vigra/stdimage.hxx"

#include "main.h"

QAssistantClient* getHelp();
QString get_global_help_path();

extern QSettings* lprofSettings;

const int instArraySize = 20;

enum instCapClass
{
    all,
    emission,
    reflective
};

void fillListedInsts(instData (&instlist)[instArraySize], bool &spyd2FirmwareOK, bool &instListChanged, instCapClass instCap);


// vcIndex is is used to track the viewing conditions
// information in the sys.hrd structure and is also saved
// off into the setting system when tracking view
// condition setting for profiles.

enum vcIndex
{
   iso3664p2,
   cie1161995,
   iso3664p1,
   brightOutdoors,
   cutSheet,
   iso3664p2_pcs,
   sRGB,
   sRGBannexD,
   iso9241,
   projDim,
   projDark,
   userdef
};

// VCStringValues are used in the ProfileParms dialogs
// The string values below correspond to the cvIndex
// values above and when adding new values there must
// also be a new vcIndex value added.  To insure that
// the setting system will work correctly after adding
// a new vcIndex/VCqStringValues pairs these MUST be added
// after any existing values.

const QString VCqStringValues [] =
{
    QTranslator::tr("ISO-3664 P2 Practical Reflection Print"),
    QTranslator::tr("CIE 116-1995 dE color difference metric"),
    QTranslator::tr("ISO-3664 P1 Critical Print Evaluation Environment"),
    QTranslator::tr("Original scene - Bright Outdoors"),
    QTranslator::tr("Cut Sheet Transparencies on viewing box"),
    QTranslator::tr("ISO-3664 P2 ICC reference medium"),
    QTranslator::tr("sRGB Monitor in darkened work environment"),
    QTranslator::tr("sRGB annex D Monitor in typical work environment"),
    QTranslator::tr("ISO 9241 Monitor in bright work environment"),
    QTranslator::tr("Projector in dim environment"),
    QTranslator::tr("Projector in dark environment"),
    QTranslator::tr("User defined")
};

typedef double RGBarray[3];

class passPickedRGB
{
public:
    RGBarray RGB;
    void getPickedRGB(passPickedRGB* rgb);
};

class dialogSize
{
public:
    void put(lprof_dialogs d, int h, int w);
};

class passSys
{
public:
    void get( PROFILERDATA* out);
    void put(PROFILERDATA &in);
};


struct target_template
{
    QString targetFile, templateFile;
    int targPos, tempPos;
};

class targetTemplate
{
public:
    target_template items;
    void put(targetTemplate &in);
};


struct monval
{
    int wppos;
    int ppos;
    int temp;
    int linked;
    BOOL rchecked;
    BOOL gchecked;
    BOOL bchecked;
    double rgamma;
    double ggamma;
    double bgamma;
    BOOL valid_mon;
};

class monVal
{
public:
    monval items;
    void get(monVal* out);
    void put(monVal &in);
};

class monCal
{
public:
    void OK(bool OK);
    void calOK(bool calOK);
};

// measurement tool stuff

class Q3VBoxLayout;

// RTTI numbers

#define imageRTTI 0xF0538
#define gridRTTI  0xF0539


// The back image

class ImageItem: public Q3CanvasRectangle {

public:

    ImageItem(QString ImageFile,
	      const QString MonitorProfile,
	      const QString ScannerProfile,
	      Q3Canvas *canvas );

    int rtti () const { return imageRTTI; }

    void Reload(QString ImageFile,
	        const QString MonitorProfile,
		const QString ScannerProfile,
		Q3Canvas *canvas );

    QImage image;
    QPixmap pixmap, proof;
    double sx, sy;
    BOOL lProof;

protected:

    void drawShape( QPainter & );
    void TransformImage(const QString OutputProfile,
                        const QString InputProfile,
                        vigra::DRGBImage& p,
                        QImage& pout);


};


// Picker layout. Coords are in 0...0xffff units

#define LAYOUT_MAX	(65535.)

class GridLayout {

public:

    void ScaleTo(int w, int h, QRect& Scaled);
    int  Scale(int n, int max);

    PATCH   p;
    QRect   rect;
};




class GridItem: public Q3CanvasRectangle {


public:

    GridItem( Q3Canvas *canvas, QString TemplateFile, int SafeFrame );
    int rtti () const { return gridRTTI; }

    void Pick(QString InputProfile, QImage& Image, double sx, double sy);

    QList<GridLayout> Layout;
    double  SafeFrameFactor;


protected:

    int  ScaleX(int n) { return (int) ((double) (n * width())  / LAYOUT_MAX); }
    int  ScaleY(int n) { return (int) ((double) (n * height()) / LAYOUT_MAX); }

    void ComputePatchRects(QRect& OurViewport,
                           GridLayout* Patch,
                           QRect& Frame,
                           QRect& HotZone);

    void ComputeViewport(QRect& OurViewport, double sx, double sy);
    void drawShape(QPainter & );
};


// Statistics

class Statistic {

public:
    Statistic() { clear(); }
    void clear(void) { n = x = x2 = 0; }
    void Add(double v) { x += v; x2 += (v * v); n  += 1.0;}
    double Std(void)	  { return sqrt((n * x2 - x * x) / (n*(n-1))); }
    double Mean(void)	  { return x/n; }

protected:
    double n, x, x2;
};


// end measurment tool stuff

struct checkerparms
{
    QString profile;
    QString msheet;
    QString ref_dir;
    double cur_ref;
    QString mon_profile_dir;
    QString mon_profile_file_name;
};

class checkerP
{
public:
    checkerparms items;
    void get(checkerP &out);
};

struct ref_file_parms
{
    QString temp_dir;
    QString template_dir;
    QString ref_dir;
    QString home_dir;
    QSettings *settings;
};

class ref_file
{
public:
    ref_file_parms items;
    void get(ref_file &out);
};    

class lprofMain;

class FigureEditor : public Q3CanvasView {

    Q_OBJECT

public:

    FigureEditor(lprofMain &main_dialog, Q3Canvas& c, QWidget* parent=0, const char* name=0, Qt::WFlags f=0) : Q3CanvasView(&c,parent,name,f), maindialog(main_dialog) {}
    void clear();
    void resizeEvent( QResizeEvent* e );

protected:
    void contentsMousePressEvent(QMouseEvent*);

signals:
    void status(const QString&);
    void WPPointsSelected();

protected:
    FigureEditor& operator=( const FigureEditor& rhs );

    GridItem* moving;
    GridItem* sizing;
    GridItem* unshearing;
    QPoint moving_start;
    lprofMain &maindialog;

};

namespace Ui { class lprofMainBase; } ;

class lprofMain : public QDialog
{
    Q_OBJECT

public:
    lprofMain( QWidget* parent = 0);
    virtual ~lprofMain();

    FigureEditor*   FigEditor;

public slots:
    virtual void slotPickRGBPatch();
    virtual void slotUpdateProfileParmsText();
    virtual void setRGBpatchValues();
    void templateFileCopied( Q3NetworkOperation *op );
    void zeroProgressBars();
    virtual void AddTemplate();
    virtual void slotPick();

private slots:
    virtual void ProfIDpushButton_clicked();
    virtual void ProfileParmsbutton();
    virtual void monitorvalues_clicked();
    virtual void ProfChecker_clicked();
    virtual void setProfileIDText();
    virtual void setProfileParmsText();
    virtual void slotProof();
    virtual void slotHelpButton();
    virtual void slotMeasure();
    virtual void slotMeasureTypeChanged();
    void slotProofProfilesChanged();
    virtual void Reload();
    virtual void slotLoad();
    virtual void slotTargetRefChanged();
    void safeFarmeChanged();
    virtual void OutputFileNameChanged();
    virtual void Go_button_clicked();
    virtual void slotSelectOutputFile();
    virtual void slotIsAllReady();
    void slotChangeStrategy();
    void slotTargetChanged();
    virtual void slotInstallReference();
    void slotEnablecomboBoxDevice();
    void slotUpdateInst();
    virtual void tabChanged();
    virtual void OutputFileNameChangedMon();

protected:

    void SaveIT8(const char *fileName);
    void LoadIT8(const char *fileName);
    void DumpResultsToIT8(QList<GridLayout>& Layout);
    void DoMonitorProfile();
    void init_mon();
    void saveProfileDetails();
    void templateFilesExist();
    void ReferenceFileExists();

    Q3Canvas*         Canvas;
    Q3VBoxLayout* CanvasFrameLayout;
    GridItem*         TheGrid;
    ImageItem*     TheImage;

    QString           CurrentTemplate;
    double             CurrentSafeFrame;

    QString           fn;

private:
    void ValuesToLabels();
    void ValuesToControls();
    void SlidersToValues();
    void ControlsToValues();
    bool validateFileName(QString &fn);
    BOOL LoadConfig();
    void SaveConfig();
    void DoProfile();
    virtual void SetOutputFile( const QString OutputFile );
    void FillResultsGrid(void);
    void RenderGamma();
    void ProofGamma();
    void init_measurement_tool();
    void SetGoButton(QString Caption, BOOL lEnable);
    void SetGoButton_2(QString Caption, BOOL lEnable);
    void DoScannerProfile();
    void init_targetlist();
    virtual void getConfigParmsandID(QString profile);
    void Create_Config_Dir(const QString a);
   
    void updateInstSettings();
    void populatePortandInstListView();

    Ui::lprofMainBase * ui;

signals:
    void showProfileParms();
    void hideProfileParms();
};

#endif // LPROFMAIN_H
