1 #ifndef _KVI_HTMLDIALOG_H_
2 #define _KVI_HTMLDIALOG_H_
3 //=============================================================================
4 //
5 //   File : KviHtmlDialog.h
6 //   Creation date : Wed 03 Jan 2007 03:36:36 by Szymon Stefanek
7 //
8 //   This file is part of the KVIrc IRC Client distribution
9 //   Copyright (C) 2007-2010 Szymon Stefanek <pragma at kvirc dot net>
10 //
11 //   This program is FREE software. You can redistribute it and/or
12 //   modify it under the terms of the GNU General Public License
13 //   as published by the Free Software Foundation; either version 2
14 //   of the License, or (at your option) any later version.
15 //
16 //   This program is distributed in the HOPE that it will be USEFUL,
17 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
18 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19 //   See the GNU General Public License for more details.
20 //
21 //   You should have received a copy of the GNU General Public License
22 //   along with this program. If not, write to the Free Software Foundation,
23 //   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 //
25 //=============================================================================
26 
27 #include "KviQString.h"
28 #include "kvi_settings.h"
29 
30 #include <QPixmap>
31 #include <QDialog>
32 #include <QUrl>
33 #include <QTextBrowser>
34 #include <QHash>
35 #include <QPixmap>
36 
37 class KviHtmlDialogData
38 {
39 public:
KviHtmlDialogData()40 	KviHtmlDialogData()
41 	{
42 		m_pDoc = new QTextDocument();
43 	};
~KviHtmlDialogData()44 	~KviHtmlDialogData() { delete m_pDoc; };
45 
addImageResource(const QString & key,const QPixmap & pix)46 	void addImageResource(const QString & key, const QPixmap & pix)
47 	{
48 		if(!m_pDoc)
49 			m_pDoc = new QTextDocument();
50 		QUrl url;
51 		url.setPath(key);
52 		m_pDoc->addResource(2, url, pix);
53 	}
54 
addHtmlResource(const QString key,const QString value)55 	void addHtmlResource(const QString key, const QString value)
56 	{
57 		htmlResource.insert(key, value);
58 	}
59 
60 	// input
61 
62 	// mandatory fields
63 	enum Flags
64 	{
65 		ForceMinimumSize = 1
66 	};
67 	int iFlags;         // da flags :)
68 	int iDefaultButton; // the button to use when Enter is pressed (1,2 or 3)
69 	int iCancelButton;  // the button to use when Esc is pressed (1,2 or 3)
70 	QString szHtmlText; // Shouldn't be empty :D
71 
72 	// optional fields
73 	QString szCaption;        // KVIrc is used when this is empty
74 	QString szUpperLabelText; // no label is shown if this is empty
75 	QString szLowerLabelText; // no label is shown if this is empty
76 	QString szButton1Text;    // OK is used if this is empty
77 	QString szButton2Text;    // no button is shown if this is empty
78 	QString szButton3Text;    // no button is shown if this is empty
79 	QTextDocument * m_pDoc;
80 	QHash<QString, QString> htmlResource;
81 	int iMinimumWidth;
82 	int iMinimumHeight;
83 
84 	QPixmap pixIcon; // may be null
85 
86 	// output
87 	int iSelectedButton; // returns 1,2 or 3
88 };
89 
90 class KviTextBrowser : public QTextBrowser
91 {
92 public:
KviTextBrowser(QWidget * par,KviHtmlDialogData * ht)93 	KviTextBrowser(QWidget * par, KviHtmlDialogData * ht)
94 	    : QTextBrowser(par), m_pHt(ht){};
~KviTextBrowser()95 	~KviTextBrowser(){};
loadResource(int type,const QUrl & name)96 	virtual QVariant loadResource(int type, const QUrl & name)
97 	{
98 		QString p = m_pHt->htmlResource.value(name.path());
99 		qDebug("resource %s type %d and page %s", name.path().toUtf8().data(), type, p.toUtf8().data());
100 		if(!p.isEmpty())
101 			return QVariant(p);
102 		else
103 			return QVariant();
104 
105 		//return QTextBrowser::loadResource(type,name);
106 	}
107 
108 protected:
109 	KviHtmlDialogData * m_pHt;
110 };
111 
112 class KVIRC_API KviHtmlDialog : public QDialog
113 {
114 	Q_OBJECT
115 public:
116 	// the dialog does NOT delete this structure and assumes that
117 	// it remains alive until the dialog closes (i.e. it may access
118 	// the structure in the destructor
119 	KviHtmlDialog(QWidget * pParent, KviHtmlDialogData * pData);
120 	~KviHtmlDialog();
121 
122 protected:
123 	KviHtmlDialogData * m_pData;
124 
125 public:
126 	// displays the dialog as modal and returns 1,2 or 3
127 	static int display(QWidget * pParent, KviHtmlDialogData * pData);
128 protected slots:
129 	void button1Pressed();
130 	void button2Pressed();
131 	void button3Pressed();
132 
133 protected:
134 	virtual void reject();
135 };
136 
137 #endif //!_KVI_HTMLDIALOG_H_
138