1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
|
//=============================================================================
//
// File : managementdialog.cpp
// Created on Fri 08 Apr 2005 14:54:56 by Szymon Stefanek
//
// This file is part of the KVIrc IRC Client distribution
// Copyright (C) 2005 Szymon Stefanek <pragma at kvirc dot 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 opinion) 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 Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
//=============================================================================
#include "managementdialog.h"
#include "kvi_listview.h"
#include "kvi_locale.h"
#include "kvi_frame.h"
#include "kvi_iconmanager.h"
#include "kvi_kvs_scriptaddonmanager.h"
#include "kvi_window.h"
#include "kvi_filedialog.h"
#include "kvi_fileutils.h"
#include "kvi_kvs_script.h"
#include "kvi_sourcesdate.h"
#include <qpushbutton.h>
#include <qlayout.h>
#include <qapplication.h>
#include <qtooltip.h>
#include <qlineedit.h>
#include <qlabel.h>
#include <qmessagebox.h>
#include <qframe.h>
#include "kvi_tal_scrollview.h"
#ifdef COMPILE_USE_QT4
#include <q3header.h>
#else
#include <qheader.h>
#endif
#include "kvi_draganddrop.h"
#include <qpainter.h>
#include <qpixmap.h>
#include <qmessagebox.h>
#include <qevent.h>
KviScriptManagementDialog * KviScriptManagementDialog::m_pInstance = 0;
extern QRect g_rectManagementDialogGeometry;
#define LVI_ICON_SIZE 32
#define LVI_BORDER 4
#define LVI_SPACING 8
#define LVI_MINIMUM_TEXT_WIDTH 300
#define LVI_MINIMUM_CELL_WIDTH (LVI_MINIMUM_TEXT_WIDTH + LVI_BORDER + LVI_ICON_SIZE + LVI_SPACING + LVI_BORDER)
KviScriptAddonListViewItem::KviScriptAddonListViewItem(KviTalListView * v,KviKvsScriptAddon * a)
: KviTalListViewItem(v,"")
{
m_pAddon = new KviKvsScriptAddon(*a);
m_pListView = v;
QString t = "<nobr><b>";
t += a->visibleName();
t += "</b> [";
t += a->version();
t += "]";
t += " <font color=\"#a0a0a0\">[";
t += a->name();
t += "]</font></nobr>";
t += "<br><nobr><font size=\"-1\">";
t += a->description();
t += "</font></nobr>";
m_szKey = a->visibleName().upper();
#ifdef COMPILE_USE_QT4
m_pText = new QTextDocument();
m_pText->setHtml(t);
m_pText->setDefaultFont(v->font());
#else
m_pText = new QSimpleRichText(t,v->font());
#endif
QPixmap * p = a->icon();
m_pIcon = p ? new QPixmap(*p) : new QPixmap(LVI_ICON_SIZE,LVI_ICON_SIZE);
}
KviScriptAddonListViewItem::~KviScriptAddonListViewItem()
{
delete m_pIcon;
delete m_pText;
delete m_pAddon;
}
QString KviScriptAddonListViewItem::key(int,bool) const
{
return m_szKey;
}
void KviScriptAddonListViewItem::setup()
{
KviTalListViewItem::setup();
int iWidth = m_pListView->visibleWidth();
if(iWidth < LVI_MINIMUM_CELL_WIDTH)iWidth = LVI_MINIMUM_CELL_WIDTH;
iWidth -= LVI_BORDER + LVI_ICON_SIZE + LVI_SPACING + LVI_BORDER;
#ifdef COMPILE_USE_QT4
int iHeight = m_pText->size().height() + (2 * LVI_BORDER);
#else
m_pText->setWidth(iWidth);
int iHeight = m_pText->height() + (2 * LVI_BORDER);
#endif
if(iHeight < (LVI_ICON_SIZE + (2 * LVI_BORDER)))iHeight = LVI_ICON_SIZE + (2 * LVI_BORDER);
setHeight(iHeight+2);
}
void KviScriptAddonListViewItem::paintCell(QPainter * p,const QColorGroup & cg,int column,int width,int align)
{
#ifdef COMPILE_USE_QT4
if (isSelected())
{
QColor col(m_pListView->palette().highlight());
col.setAlpha(127);
p->setBrush(col);
p->drawRect(0, 0, m_pListView->visibleWidth(), height());
}
p->drawPixmap(LVI_BORDER,LVI_BORDER,*m_pIcon);
int afterIcon = LVI_BORDER + LVI_ICON_SIZE + LVI_SPACING;
int www = m_pListView->visibleWidth() - (afterIcon + LVI_BORDER);
p->translate(afterIcon,LVI_BORDER);
m_pText->setPageSize(QSizeF(www,height() - (LVI_BORDER * 2)));
m_pText->drawContents(p);
#else
p->drawPixmap(LVI_BORDER,LVI_BORDER,*m_pIcon);
int afterIcon = LVI_BORDER + LVI_ICON_SIZE + LVI_SPACING;
int www = m_pListView->visibleWidth() - (afterIcon + LVI_BORDER);
m_pText->setWidth(www);
if(isSelected())
{
QColorGroup cg2(cg);
cg2.setColor(QColorGroup::HighlightedText,cg.text());
m_pText->draw(p,afterIcon,LVI_BORDER,QRect(afterIcon,LVI_BORDER,www,height() - (LVI_BORDER * 2)),cg2);
} else {
m_pText->draw(p,afterIcon,LVI_BORDER,QRect(afterIcon,LVI_BORDER,www,height() - (LVI_BORDER * 2)),cg);
}
#endif
}
KviScriptAddonListView::KviScriptAddonListView(QWidget * pParent)
: KviListView(pParent)
{
QPixmap * p = g_pIconManager->getImage("kvi_dialog_addons.png");
if(p)setBackgroundOverlayPixmap(p,Qt::AlignRight | Qt::AlignBottom);
setSelectionMode(Single);
header()->hide();
int iWidth = visibleWidth();
if(iWidth < LVI_MINIMUM_CELL_WIDTH)iWidth = LVI_MINIMUM_CELL_WIDTH;
addColumn("",iWidth);
setSorting(0,true);
}
KviScriptAddonListView::~KviScriptAddonListView()
{
}
void KviScriptAddonListView::resizeEvent(QResizeEvent * e)
{
KviListView::resizeEvent(e);
int iWidth = visibleWidth();
if(iWidth < LVI_MINIMUM_CELL_WIDTH)iWidth = LVI_MINIMUM_CELL_WIDTH;
setColumnWidth(0,iWidth);
}
KviScriptManagementDialog::KviScriptManagementDialog(QWidget * p)
: QDialog(p,"" /*,WType_TopLevel | WStyle_Customize | WStyle_Title | WStyle_StaysOnTop | WStyle_DialogBorder*/)
{
setCaption(__tr2qs("Manage Script-Based Addons"));
setIcon(*(g_pIconManager->getSmallIcon(KVI_SMALLICON_ADDONS)));
setModal(true);
m_pInstance = this;
QGridLayout * g = new QGridLayout(this,11,3,4,5);
/*QLabel * lb = new QLabel(this);
lb->setFrameStyle(QFrame::WinPanel | QFrame::Sunken);
g->addMultiCellWidget(lb,0,10,0,0);
QPixmap * pix = g_pIconManager->getImage("kvi_dialog_addons.png");
if(pix)
{
lb->setPixmap(*pix);
lb->setFixedWidth(pix->width());
}
lb->setBackgroundColor(Qt::black);
lb->setAlignment(Qt::AlignBottom | Qt::AlignRight);
*/
m_pListView = new KviScriptAddonListView(this);
g->addMultiCellWidget(m_pListView,0,10,1,1);
m_pConfigureButton = new QPushButton(__tr2qs("Configure"),this);
connect(m_pConfigureButton,SIGNAL(clicked()),this,SLOT(configureScript()));
g->addWidget(m_pConfigureButton,0,2);
m_pHelpButton = new QPushButton(__tr2qs("Show Help"),this);
connect(m_pHelpButton,SIGNAL(clicked()),this,SLOT(showScriptHelp()));
g->addWidget(m_pHelpButton,1,2);
g->addRowSpacing(2,40);
m_pUninstallButton = new QPushButton(__tr2qs("Uninstall"),this);
connect(m_pUninstallButton,SIGNAL(clicked()),this,SLOT(uninstallScript()));
g->addWidget(m_pUninstallButton,3,2);
g->addRowSpacing(4,15);
QFrame *f = new QFrame(this);
f->setFrameStyle(QFrame::HLine | QFrame::Sunken);
g->addWidget(f,5,2);
g->addRowSpacing(6,15);
m_pInstallButton = new QPushButton(__tr2qs("Install Addon..."),this);
connect(m_pInstallButton,SIGNAL(clicked()),this,SLOT(installScript()));
g->addWidget(m_pInstallButton,7,2);
m_pGetScriptsButton = new QPushButton(__tr2qs("More Addons..."),this);
connect(m_pGetScriptsButton,SIGNAL(clicked()),this,SLOT(getMoreScripts()));
g->addWidget(m_pGetScriptsButton,8,2);
QPushButton * b = new QPushButton(__tr2qs("Close"),this);
connect(b,SIGNAL(clicked()),this,SLOT(closeClicked()));
g->addWidget(b,10,2);
g->setRowStretch(9,1);
g->setColStretch(1,1);
fillListView();
currentChanged(0);
connect(m_pListView,SIGNAL(currentChanged(KviTalListViewItem *)),this,SLOT(currentChanged(KviTalListViewItem *)));
//currentToolBarChanged();
if(g_rectManagementDialogGeometry.y() < 5)
{
g_rectManagementDialogGeometry.setY(5);
}
resize(g_rectManagementDialogGeometry.width(),
g_rectManagementDialogGeometry.height());
move(g_rectManagementDialogGeometry.x(),
g_rectManagementDialogGeometry.y());
}
KviScriptManagementDialog::~KviScriptManagementDialog()
{
g_rectManagementDialogGeometry = QRect(pos().x(),pos().y(),size().width(),size().height());
//KviActionManager::instance()->customizeToolBarsDialogDestroyed();
m_pInstance = 0;
}
void KviScriptManagementDialog::fillListView()
{
m_pListView->clear();
KviPointerHashTable<QString,KviKvsScriptAddon> * d = KviKvsScriptAddonManager::instance()->addonDict();
if(!d)return;
KviPointerHashTableIterator<QString,KviKvsScriptAddon> it(*d);
KviScriptAddonListViewItem * item;
while(KviKvsScriptAddon * a = it.current())
{
item = new KviScriptAddonListViewItem(m_pListView,a);
++it;
}
}
void KviScriptManagementDialog::currentChanged(KviTalListViewItem *)
{
KviScriptAddonListViewItem * it = (KviScriptAddonListViewItem *)m_pListView->currentItem();
if(!it)
{
m_pConfigureButton->setEnabled(false);
m_pUninstallButton->setEnabled(false);
m_pHelpButton->setEnabled(false);
} else {
m_pConfigureButton->setEnabled(!(it->addon()->configureCallbackCode().isEmpty()));
m_pHelpButton->setEnabled(!(it->addon()->helpCallbackCode().isEmpty()));
m_pUninstallButton->setEnabled(true);
}
}
void KviScriptManagementDialog::showScriptHelp()
{
KviScriptAddonListViewItem * it = (KviScriptAddonListViewItem *)m_pListView->currentItem();
if(!it)return;
if(it->addon()->helpCallbackCode().isEmpty())return;
it->addon()->executeHelpCallback(g_pActiveWindow);
}
void KviScriptManagementDialog::configureScript()
{
KviScriptAddonListViewItem * it = (KviScriptAddonListViewItem *)m_pListView->currentItem();
if(!it)return;
if(it->addon()->configureCallbackCode().isEmpty())return;
it->addon()->executeConfigureCallback(g_pActiveWindow);
}
void KviScriptManagementDialog::uninstallScript()
{
KviScriptAddonListViewItem * it = (KviScriptAddonListViewItem *)m_pListView->currentItem();
if(!it)return;
QString txt = "<p>";
txt += __tr2qs("Do you really want to uninstall the addon \"%1\" ?").arg(it->addon()->visibleName());
txt += "</p>";
if(QMessageBox::question(this,
__tr2qs("Confirm addon uninstallation"),txt,__tr2qs("&Yes"),__tr2qs("&No"),0,1) != 0)return;
KviKvsScriptAddonManager::instance()->unregisterAddon(it->addon()->name(),g_pActiveWindow);
fillListView();
currentChanged(0);
}
void KviScriptManagementDialog::getMoreScripts()
{
KviKvsScript::run("openurl http://www.kvirc.net/?id=addons&version=" KVI_VERSION "." KVI_SOURCES_DATE,g_pActiveWindow);
}
void KviScriptManagementDialog::installScript()
{
QString buffer;
if(!KviFileDialog::askForOpenFileName(buffer,__tr2qs("Please select the addon installation file"),QString::null,"install.kvs",false,true))return;
buffer.replace("\\","\\\\");
QString szCmd = "parse \"";
szCmd += buffer;
szCmd += "\"";
KviKvsScript::run(szCmd,g_pActiveWindow);
fillListView();
currentChanged(0);
m_pListView->publicUpdateContents();
//m_pListView->triggerUpdate();
}
void KviScriptManagementDialog::showEvent(QShowEvent * e)
{
// QRect r = parentWidget() ? parentWidget()->rect() : QApplication::desktop()->rect();
// int x = (r.width() - width()) / 2;
// int y = (r.height() - height()) / 2;
// move(x,y);
}
void KviScriptManagementDialog::closeClicked()
{
delete this;
}
void KviScriptManagementDialog::cleanup()
{
if(!m_pInstance)return;
delete m_pInstance;
m_pInstance = 0;
}
void KviScriptManagementDialog::display()
{
if(m_pInstance)return;
m_pInstance = new KviScriptManagementDialog(g_pFrame);
m_pInstance->show();
}
void KviScriptManagementDialog::closeEvent(QCloseEvent * e)
{
e->ignore();
delete this;
}
|