1 /***************************************************************************
2 * Copyright (C) 2005-2008 by Felix Geyer *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; version 2 of the License. *
8 * This program is distributed in the hope that it will be useful, *
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
11 * GNU General Public License for more details. *
13 * You should have received a copy of the GNU General Public License *
14 * along with this program; if not, write to the *
15 * Free Software Foundation, Inc., *
16 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
17 ***************************************************************************/
19 #include <QDesktopWidget>
20 #include "AutoTypeDlg.h"
22 bool AutoTypeDlg::dialogVisible = false;
24 AutoTypeDlg::AutoTypeDlg(QList<IEntryHandle*> entries, QList<int> numbers, bool wasLocked) : pWasLocked(wasLocked){
25 Q_ASSERT(!dialogVisible);
29 setAttribute(Qt::WA_DeleteOnClose);
30 setWindowFlags(windowFlags()|Qt::WindowStaysOnTopHint);
31 setGeometry( QRect(QApplication::desktop()->screenGeometry(QCursor::pos()).center() - rect().center(), size()) );
32 setWindowIcon(getIcon("keepassx_small"));
33 entryList->setAlternatingRowColors(config->alternatingRowColors());
35 bool hideUsernames = config->hideUsernames();
37 entryList->setHeaderLabels(QStringList() << tr("Group") << tr("Title"));
39 entryList->setHeaderLabels(QStringList() << tr("Group") << tr("Title") << tr("Username"));
41 QList<QTreeWidgetItem*> itemList;
42 AutoTypeEntry autoTypeEntry;
43 for (int i=0; i<entries.size(); i++){
45 cols << entries[i]->group()->title() << entries[i]->title();
47 cols << entries[i]->username();
49 QTreeWidgetItem* widgetItem = new QTreeWidgetItem(cols);
50 itemList.append(widgetItem);
51 autoTypeEntry.dbHandle = entries[i];
52 autoTypeEntry.nr = numbers[i];
53 itemToEntry.insert(widgetItem, autoTypeEntry);
56 qSort(itemList.begin(), itemList.end(), itemLessThan);
57 for (int i=0; i<itemList.size(); i++)
58 entryList->addTopLevelItem(itemList[i]);
59 entryList->setCurrentItem(itemList[0]);
61 entryList->resizeColumnToContents(0);
62 entryList->resizeColumnToContents(1);
64 entryList->resizeColumnToContents(2);
66 entryList->setColumnWidth(0, entryList->columnWidth(0)+10);
68 entryList->setColumnWidth(1, entryList->columnWidth(1)+10);
70 connect(ButtonBox, SIGNAL(rejected()), SLOT(close()));
71 connect(entryList, SIGNAL(itemClicked(QTreeWidgetItem*,int)), SLOT(itemSelected(QTreeWidgetItem*)));
72 connect(entryList, SIGNAL(returnPressed(QTreeWidgetItem*)), SLOT(itemSelected(QTreeWidgetItem*)));
75 bool AutoTypeDlg::itemLessThan(QTreeWidgetItem* i1, QTreeWidgetItem* i2){
76 if (i1->text(0) != i2->text(0))
77 return i1->text(0) < i2->text(0);
78 else if (i1->text(1) != i2->text(1))
79 return i1->text(1) < i2->text(1);
81 return i1->text(2) < i2->text(2);
85 void AutoTypeDlg::paintEvent(QPaintEvent* event){
86 QWidget::paintEvent(event);
87 QPainter painter(this);
88 painter.setClipRegion(event->region());
89 painter.drawPixmap(QPoint(0,0),BannerPixmap);
92 void AutoTypeDlg::resizeEvent(QResizeEvent* event){
93 createBanner(&BannerPixmap,getPixmap("keepassx"),tr("Auto-Type"),width());
94 QWidget::resizeEvent(event);
97 void AutoTypeDlg::closeEvent(QCloseEvent* event) {
98 Q_ASSERT(dialogVisible);
99 dialogVisible = false;
100 QWidget::closeEvent(event);
103 bool AutoTypeDlg::event(QEvent* event){
105 int t = event->type();
106 if ( (t>=QEvent::MouseButtonPress && t<=QEvent::KeyRelease) || (t>=QEvent::HoverEnter && t<=QEvent::HoverMove) )
107 EventOccurred = true;
109 return QWidget::event(event);
112 void AutoTypeDlg::itemSelected(QTreeWidgetItem* item){
114 autoType->perform(itemToEntry[item].dbHandle, pWasLocked, itemToEntry[item].nr, pWasLocked);