• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • kdeutils
  • Sitemap
  • Contact Us
 

kgpg

keytreeview.cpp

Go to the documentation of this file.
00001 /* Copyright 2008  Rolf Eike Beer <kde@opensource.sf-tec.de>
00002  *
00003  * This program is free software; you can redistribute it and/or
00004  * modify it under the terms of the GNU General Public License as
00005  * published by the Free Software Foundation; either version 2 of
00006  * the License or (at your option) version 3 or any later version
00007  * accepted by the membership of KDE e.V. (or its successor approved
00008  * by the membership of KDE e.V.), which shall act as a proxy
00009  * defined in Section 14 of version 3 of the license.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00018  */
00019 #include "keytreeview.h"
00020 
00021 #include <QDragMoveEvent>
00022 #include <QDropEvent>
00023 
00024 #include <KMessageBox>
00025 #include <KLocale>
00026 #include <KConfigGroup>
00027 
00028 #include "kgpgitemmodel.h"
00029 #include "keylistproxymodel.h"
00030 #include "kgpgitemnode.h"
00031 #include "kgpginterface.h"
00032 
00033 KeyTreeView::KeyTreeView(QWidget *parent, KeyListProxyModel *model)
00034     : QTreeView(parent), m_proxy(model)
00035 {
00036     setModel(model);
00037     setDragEnabled(true);
00038     setDragDropMode(DragDrop);
00039     setAcceptDrops(true);
00040 }
00041 
00042 QList<KGpgNode *>
00043 KeyTreeView::selectedNodes(bool *psame, KgpgCore::KgpgItemType *pt) const
00044 {
00045     QModelIndexList selidx = selectedIndexes();
00046     QList<KGpgNode *> ndlist;
00047     KgpgItemType tp = 0;
00048     bool sametype = true;
00049 
00050     if (selidx.count() == 0) {
00051         if (pt != NULL)
00052             *pt = tp;
00053         if (psame != NULL)
00054             *psame = sametype;
00055         return ndlist;
00056     }
00057 
00058     tp = m_proxy->nodeForIndex(selidx[0])->getType();
00059 
00060     for (int i = 0; i < selidx.count(); i++) {
00061         if (selidx[i].column() != 0)
00062             continue;
00063         KGpgNode *nd = m_proxy->nodeForIndex(selidx[i]);
00064 
00065         if (nd->getType() != tp) {
00066             tp |= nd->getType();
00067             sametype = false;
00068         }
00069 
00070         ndlist << nd;
00071     }
00072 
00073     if (pt != NULL)
00074         *pt = tp;
00075     if (psame != NULL)
00076         *psame = sametype;
00077     return ndlist;
00078 }
00079 
00080 KGpgNode *
00081 KeyTreeView::selectedNode() const
00082 {
00083     QModelIndexList selidx = selectedIndexes();
00084 
00085     if (selidx.isEmpty())
00086         return NULL;
00087 
00088     return m_proxy->nodeForIndex(selidx[0]);
00089 }
00090 
00091 void
00092 KeyTreeView::selectNode(KGpgNode *nd)
00093 {
00094     if (nd == NULL)
00095         return;
00096 
00097     QModelIndex idx = m_proxy->nodeIndex(nd);
00098 
00099     selectionModel()->select(idx, QItemSelectionModel::SelectCurrent | QItemSelectionModel::Rows);
00100     selectionModel()->setCurrentIndex(idx, QItemSelectionModel::SelectCurrent | QItemSelectionModel::Rows);
00101 }
00102 
00103 void
00104 KeyTreeView::restoreLayout(KConfigGroup &cg)
00105 {
00106     QStringList cols = cg.readEntry("ColumnWidths", QStringList());
00107     int i = 0;
00108 
00109     QStringList::ConstIterator it = cols.constBegin();
00110     const QStringList::ConstIterator itEnd = cols.constEnd();
00111     for (; it != itEnd; ++it)
00112         setColumnWidth(i++, (*it).toInt());
00113 
00114     if (cg.hasKey("SortColumn")) {
00115         Qt::SortOrder order = cg.readEntry("SortAscending", true) ? Qt::AscendingOrder : Qt::DescendingOrder;
00116         sortByColumn(cg.readEntry("SortColumn", 0), order);
00117     }
00118 }
00119 
00120 void
00121 KeyTreeView::saveLayout(KConfigGroup &cg) const
00122 {
00123     QStringList widths;
00124 
00125     const int colCount = model()->columnCount();
00126 
00127     for (int i = 0; i < colCount; ++i) {
00128         widths << QString::number(columnWidth(i));
00129     }
00130     cg.writeEntry("ColumnWidths", widths);
00131 #ifdef __GNUC__
00132 #warning port me
00133 #endif
00134 /*  cg.writeEntry("SortColumn", d->sortColumn);
00135     cg.writeEntry("SortAscending", d->sortAscending);*/
00136 }
00137 
00138 void
00139 KeyTreeView::contentsDragMoveEvent(QDragMoveEvent *e)
00140 {
00141     e->setAccepted(KUrl::List::canDecode(e->mimeData()));
00142 }
00143 
00144 void
00145 KeyTreeView::contentsDropEvent(QDropEvent *o)
00146 {
00147     KUrl::List uriList = KUrl::List::fromMimeData(o->mimeData());
00148     if (!uriList.isEmpty()) {
00149         if (KMessageBox::questionYesNo(this, i18n("<p>Do you want to import file <b>%1</b> into your key ring?</p>",
00150                     uriList.first().path()), QString(), KGuiItem(i18n("Import")),
00151                     KGuiItem(i18n("Do Not Import"))) != KMessageBox::Yes)
00152             return;
00153 
00154         KgpgInterface *interface = new KgpgInterface();
00155         connect(interface, SIGNAL(importKeyFinished(KgpgInterface *, QStringList)), SLOT(slotRefreshKeys(KgpgInterface *, QStringList)));
00156         interface->importKey(uriList.first());
00157     }
00158 }
00159 
00160 void
00161 KeyTreeView::slotRefreshKeys(KgpgInterface *iface, const QStringList &keys)
00162 {
00163     iface->deleteLater();
00164     if (!keys.isEmpty())
00165         m_proxy->getModel()->refreshKeys(keys);
00166 }
00167 
00168 void
00169 KeyTreeView::startDrag(Qt::DropActions supportedActions)
00170 {
00171     QList<KGpgNode *> nodes = selectedNodes();
00172 
00173     if (nodes.isEmpty())
00174         return;
00175 
00176     KGpgNode *nd = nodes.first();
00177     QString keyid = nd->getId();
00178 
00179     if (!(nd->getType() & ITYPE_PUBLIC))
00180         return;
00181 
00182     KgpgInterface *interface = new KgpgInterface();
00183     QString keytxt = interface->getKeys(NULL, QStringList(keyid));
00184     delete interface;
00185 
00186     QMimeData *m = new QMimeData();
00187     m->setText(keytxt);
00188     QDrag *d = new QDrag(this);
00189     d->setMimeData(m);
00190     d->exec(supportedActions, Qt::IgnoreAction);
00191     // do NOT delete d.
00192 }
00193 
00194 void
00195 KeyTreeView::resizeColumnsToContents()
00196 {
00197     for (int i = m_proxy->columnCount() - 1; i >= 0; i--)
00198         resizeColumnToContents(i);
00199 }

kgpg

Skip menu "kgpg"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdeutils

Skip menu "kdeutils"
  • ark
  • kcalc
  • kcharselect
  • kdessh
  • kdf
  • kfloppy
  • kgpg
  • ktimer
  • kwallet
  • okteta
  • printer-applet
  • superkaramba
  • sweeper
Generated for kdeutils by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal