Skip to content

Commit

Permalink
Merge branch 'master' of http://code.google.com/p/qutty
Browse files Browse the repository at this point in the history
  • Loading branch information
suriyapriya committed Jul 16, 2013
2 parents a4dd4aa + 2c82381 commit e6dbef2
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 5 deletions.
46 changes: 41 additions & 5 deletions GuiCompactSettingsWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
#include <QStringList>

GuiCompactSettingsWindow::GuiCompactSettingsWindow(QWidget *parent, GuiBase::SplitType openmode)
: QDialog(parent)
: QDialog(parent),
session_list_model(NULL)
{
openMode = openmode;

Expand All @@ -32,15 +33,21 @@ GuiCompactSettingsWindow::GuiCompactSettingsWindow(QWidget *parent, GuiBase::Spl
for(auto it = qutty_mru_sesslist.mru_list.begin();
it != qutty_mru_sesslist.mru_list.end();
++it) {
completions << it->second;
if (it->second[0] == '\0')
continue;
// in 'hostname|sessname' format
completions << it->second + "|" + it->first;
}
QtCompleterWithAdvancedCompletion *c = new QtCompleterWithAdvancedCompletion(le_hostname);
c->setModel(completions);
c->popup()->setItemDelegate(new QtHostNameCompleterItemDelegate);
connect(c, SIGNAL(activated(QString)), this, SLOT(on_hostname_completion_activated(QString)));

cb_session_list = new QtComboBoxWithTreeView(this);
cb_session_list->setItemDelegate(new QtSessionTreeItemDelegate);
cb_session_list->setModel(new QtSessionTreeModel(this, qutty_config.config_list));
cb_session_list->setMaxVisibleItems(15);
session_list_model = new QtSessionTreeModel(this, qutty_config.config_list);
cb_session_list->setModel(session_list_model);
cb_session_list->setMaxVisibleItems(25);

cb_connection_type = new QComboBox(this);
cb_connection_type->setMaximumWidth(100);
Expand All @@ -50,7 +57,7 @@ GuiCompactSettingsWindow::GuiCompactSettingsWindow(QWidget *parent, GuiBase::Spl
if(qutty_config.config_list.find(QUTTY_DEFAULT_CONFIG_SETTINGS) != qutty_config.config_list.end())
{
cfg = &qutty_config.config_list[QUTTY_DEFAULT_CONFIG_SETTINGS];
cb_session_list->setCurrentText(QString(cfg->config_name));
cb_session_list->setCurrentIndex(cb_session_list->findText(QUTTY_DEFAULT_CONFIG_SETTINGS));
le_hostname->setText(QString(cfg->host));
if(cfg->protocol == PROT_TELNET)
cb_connection_type->setCurrentIndex(0);
Expand Down Expand Up @@ -141,3 +148,32 @@ void GuiCompactSettingsWindow::on_cb_session_list_activated(int n)
cb_connection_type->setCurrentIndex(1);
}
}

void GuiCompactSettingsWindow::on_hostname_completion_activated(QString str)
{
QStringList split = str.split('|');
if (split.length() > 1) {
le_hostname->setText(split[0]);

/*
* Based on the suggestion/technique from below url:
* http://www.qtcentre.org/threads/14699-QCombobox-with-QTreeView-QTreeWidget
*/
QString fullsessname = split[1];
QAbstractItemView *treeview = cb_session_list->view();
QModelIndex m_index = session_list_model->findIndexForSessionName(fullsessname);
treeview->setCurrentIndex(m_index.parent());
cb_session_list->setRootModelIndex(treeview->currentIndex());
cb_session_list->setCurrentIndex(m_index.row());
treeview->setCurrentIndex(QModelIndex());
cb_session_list->setRootModelIndex(treeview->currentIndex());

bool old = QApplication::isEffectEnabled(Qt::UI_AnimateCombo);
if (old)
QApplication::setEffectEnabled(Qt::UI_AnimateCombo, false);
cb_session_list->showPopup();
cb_session_list->hidePopup();
if (old)
QApplication::setEffectEnabled(Qt::UI_AnimateCombo, true);
}
}
23 changes: 23 additions & 0 deletions GuiCompactSettingsWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
#include "QtConfig.h"
#include "GuiSettingsWindow.h"
#include <QMessageBox>
#include <QPainter>
#include "QtComboBoxWithTreeView.h"
#include "QtSessionTreeModel.h"

class GuiMainWindow;

Expand All @@ -27,6 +29,8 @@ class GuiCompactSettingsWindow : public QDialog
QLineEdit *le_hostname;
GuiBase::SplitType openMode;

QtSessionTreeModel *session_list_model;

public:
explicit GuiCompactSettingsWindow(QWidget *parent, GuiBase::SplitType openmode = GuiBase::TYPE_LEAF);

Expand All @@ -40,6 +44,25 @@ public slots:
void on_close_clicked();
void on_details_clicked();
void on_cb_session_list_activated(int);
void on_hostname_completion_activated(QString str);
};

class QtHostNameCompleterItemDelegate : public QStyledItemDelegate
{
void paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
QStringList split = index.model()->data(index).toString().split('|');
if (split.length() != 2)
return;

if (option.state & QStyle::State_Selected)
painter->fillRect(option.rect, option.palette.highlight());
QString hostname = split[0];
QString sessname = split[1];
painter->drawText(option.rect, hostname);
painter->drawText(option.rect, Qt::AlignRight, sessname);
}
};

#endif // GUICOMPACTSETTINGSWINDOW_H
2 changes: 2 additions & 0 deletions QtCompleterWithAdvancedCompletion.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class QtCompleterWithAdvancedCompletion : public QObject
void setMaxVisibleItems(int maxItems) { maxVisibleItems = maxItems; }
void setFilterMode(FilterMode mode) { filterMode = mode; }

QListView *popup() const { return popuplist; }

protected:
bool eventFilter(QObject *o, QEvent *e);

Expand Down
25 changes: 25 additions & 0 deletions QtSessionTreeModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,31 @@ int QtSessionTreeModel::columnCount(const QModelIndex &parent) const
return 1; // only 1 column - session_name
}

QModelIndex QtSessionTreeModel::findIndexForSessionName(QString fullsessname) const
{
QStringList dirname = fullsessname.split(QUTTY_SESSION_NAME_SPLIT);
QModelIndex par;
QModelIndex ch;
for(auto it = dirname.begin(); it != dirname.end(); it++) {
bool isfound = false;
for(int r = 0; r < rowCount(par); r++) {
ch = index(r, 0, par);
if (!ch.isValid())
continue;
QtSessionTreeItem *chitem = static_cast<QtSessionTreeItem*>(ch.internalPointer());
if (chitem->getSessionName() == *it) {
par = ch;
isfound = true;
break;
}
}
if (!isfound) {
return QModelIndex();
}
}
return ch;
}

QVariant QtSessionTreeModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid() || role != Qt::DisplayRole)
Expand Down
2 changes: 2 additions & 0 deletions QtSessionTreeModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class QtSessionTreeModel : public QAbstractItemModel
int rowCount(const QModelIndex &parent = QModelIndex()) const;
int columnCount(const QModelIndex &parent = QModelIndex()) const;

QModelIndex findIndexForSessionName(QString fullsessname) const;

private:
QtSessionTreeItem *rootItem;
};
Expand Down

0 comments on commit e6dbef2

Please sign in to comment.