#include "treeview.h"
#include <QAction>
#include <QInputDialog>
#include "TreeModel.h"
TreeView::TreeView(QMenu* menu, QWidget *parent)
: QTreeView(parent)
, menu_(menu)
{
createContextMenu();
this->setDragEnabled(true);
this->setAcceptDrops(true);
this->setDropIndicatorShown(true);
this->setHeaderHidden(true);
}
void TreeView::createContextMenu()
{
mnuAdd = new QAction(tr("Add"), this);
mnuRemove = new QAction(tr("Remove"), this);
mnuExpand = new QAction(tr("Expand all"), this);
mnuCollapse = new QAction(tr("Collapse all"), this);
connect(mnuAdd, SIGNAL(triggered()), this, SLOT(mnuAddClick()));
connect(mnuRemove, SIGNAL(triggered()), this, SLOT(mnuRemoveClick()));
connect(mnuExpand, SIGNAL(triggered()), this, SLOT(mnuExpandClick()));
connect(mnuCollapse, SIGNAL(triggered()), this, SLOT(mnuCollapseClick()));
}
QModelIndexList TreeView::selectedIndexes() const
{
return QTreeView::selectedIndexes();
}
void TreeView::mnuAddClick()
{
bool ok;
QString text = QInputDialog::getText(this, tr("QInputDialog::getText()"),
tr("Group name:"), QLineEdit::Normal,
"", &ok);
if (ok && !text.isEmpty())
{
TreeModel* mm = (TreeModel*)this->model();
mm->addNewGroup(text);
}
}
void TreeView::mnuRemoveClick()
{
QModelIndex idx = currentIndex();
if( !idx.isValid() )
return;
TreeItem* ti = static_cast<TreeItem*>(idx.internalPointer());
GroupTreeItem* gti = dynamic_cast<GroupTreeItem*>(ti);
if( !gti )
return;
if( gti->childCount() > 0 )
{
QMessageBox::warning(this,"Cannot remove group","Group cannot be removed, because it contain other groups or PuTTY sessions.");
}else{
const Group* g = gti->getGroupInfo();
TreeModel* mm = (TreeModel*)this->model();
mm->removeGroup(g->groupId());
}
}
void TreeView::mnuExpandClick()
{
expandAll();
}
void TreeView::mnuCollapseClick()
{
collapseAll();
}
void TreeView::contextMenuEvent(QContextMenuEvent *event)
{
QMenu menu(this);
menu.addActions( menu_->actions() );
menu.addSeparator();
menu.addAction(mnuExpand);
menu.addAction(mnuCollapse);
menu.addSeparator();
menu.addAction(mnuAdd);
menu.addAction(mnuRemove);
menu.exec(event->globalPos());
}