/**
     * @brief Handles ListWidget layoutChanged() signal
     */
    void ConnectionsDialog::listWidget_layoutChanged()
    {
        int count = _listWidget->topLevelItemCount();

        // Make childrens toplevel again. This is a bad, but quickiest item reordering
        // implementation.
        for(int i = 0; i < count; i++)
        {
            ConnectionListWidgetItem * item = (ConnectionListWidgetItem *) _listWidget->topLevelItem(i);
            if (item->childCount() > 0) {
                ConnectionListWidgetItem *childItem = (ConnectionListWidgetItem *) item->child(0);
                item->removeChild(childItem);
                _listWidget->insertTopLevelItem(++i, childItem);
                _listWidget->setCurrentItem(childItem);
                break;
            }
        }

        count = _listWidget->topLevelItemCount();
        SettingsManager::ConnectionSettingsContainerType items;
        for(int i = 0; i < count; i++)
        {
            ConnectionListWidgetItem * item = (ConnectionListWidgetItem *) _listWidget->topLevelItem(i);
            items.push_back(item->connection());
        }

        _settingsManager->reorderConnections(items);
    }
    void MainWindow::updateConnectionsMenu()
    {
        _connectionsMenu->clear();
        int number = 1;
        // Populate list with connections
        SettingsManager::ConnectionSettingsContainerType connections = AppRegistry::instance().settingsManager()->connections();
        for(SettingsManager::ConnectionSettingsContainerType::const_iterator it = connections.begin(); it!= connections.end(); ++it) {
            ConnectionSettings *connection = *it;
            QAction *action = new QAction(QtUtils::toQString(connection->getReadableName()), this);
            action->setData(QVariant::fromValue(connection));

            if (number <= 9 && !AppRegistry::instance().settingsManager()->disableConnectionShortcuts()) {
                action->setShortcut(QKeySequence(QString("Alt+").append(QString::number(number))));
            }

            _connectionsMenu->addAction(action);
            ++number;
        }

        if (AppRegistry::instance().settingsManager()->connections().size() > 0)
            _connectionsMenu->addSeparator();

        // Connect action
        QAction *connectAction = new QAction("&Manage Connections...", this);
        connectAction->setIcon(GuiRegistry::instance().connectIcon());
        connectAction->setToolTip("Connect to MongoDB");
        VERIFY(connect(connectAction, SIGNAL(triggered()), this, SLOT(manageConnections())));

        _connectionsMenu->addAction(connectAction);
    }
 void MainWindow::open()
 {
     QueryWidget *wid = _workArea->currentQueryWidget();
     if (wid) {
         wid->openFile();
     }
     else {
         SettingsManager::ConnectionSettingsContainerType connections = AppRegistry::instance().settingsManager()->connections();
         if (connections.size() == 1) {
             ScriptInfo inf = ScriptInfo(QString());
             if (inf.loadFromFile()) {
                 _app->openShell(connections.at(0), inf);
             }
         }
     }
 }
    /**
     * @brief Creates dialog
     */
    ConnectionsDialog::ConnectionsDialog(QWidget* parent)
        : QDialog(parent)
    {
        using namespace translations;

        setWindowIcon(GuiFactory::instance().connectIcon());

        // Remove help button (?)
        setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);

        listWidget_ = new QTreeWidget;
        listWidget_->setIndentation(5);

        QStringList colums;
        colums << trName << trAddress;
        listWidget_->setHeaderLabels(colums);

        //listWidget_->header()->setSectionResizeMode(0, QHeaderView::Stretch);
        //listWidget_->header()->setSectionResizeMode(1, QHeaderView::Stretch);

        //listWidget_->setViewMode(QListView::ListMode);
        listWidget_->setContextMenuPolicy(Qt::ActionsContextMenu);
        listWidget_->setIndentation(15);
        listWidget_->setSelectionMode(QAbstractItemView::SingleSelection); // single item can be draged or droped
        listWidget_->setSelectionBehavior(QAbstractItemView::SelectRows);

        //listWidget_->setDragEnabled(true);
        //listWidget_->setDragDropMode(QAbstractItemView::InternalMove);
        setMinimumSize(QSize(min_width, min_height));
        VERIFY(connect(listWidget_, &QTreeWidget::itemDoubleClicked, this, &ConnectionsDialog::accept));
        VERIFY(connect(listWidget_, &QTreeWidget::itemSelectionChanged, this, &ConnectionsDialog::connectionSelectChange));

        QDialogButtonBox *buttonBox = new QDialogButtonBox(this);
        buttonBox->setOrientation(Qt::Horizontal);
        buttonBox->setStandardButtons(QDialogButtonBox::Cancel | QDialogButtonBox::Ok);
        buttonBox->button(QDialogButtonBox::Ok)->setIcon(GuiFactory::instance().serverIcon());
        acButton_ = buttonBox->button(QDialogButtonBox::Ok);
        acButton_->setEnabled(false);

        VERIFY(connect(buttonBox, &QDialogButtonBox::accepted, this, &ConnectionsDialog::accept));
        VERIFY(connect(buttonBox, &QDialogButtonBox::rejected, this, &ConnectionsDialog::reject));

        QHBoxLayout *bottomLayout = new QHBoxLayout;
        bottomLayout->addWidget(buttonBox);

        QToolBar *savebar = new QToolBar;

        QAction *addB = new QAction(GuiFactory::instance().loadIcon(), trAddConnection, savebar);
        typedef void(QAction::*trig)(bool);
        VERIFY(connect(addB, static_cast<trig>(&QAction::triggered), this, &ConnectionsDialog::add));
        savebar->addAction(addB);

        QAction *addc = new QAction(GuiFactory::instance().clusterIcon(), trAddClusterConnection, savebar);
        typedef void(QAction::*trig)(bool);
        VERIFY(connect(addc, static_cast<trig>(&QAction::triggered), this, &ConnectionsDialog::addCls));
        savebar->addAction(addc);

        QAction *rmB = new QAction(GuiFactory::instance().removeIcon(), trRemoveConnection, savebar);
        VERIFY(connect(rmB, static_cast<trig>(&QAction::triggered), this, &ConnectionsDialog::remove));
        savebar->addAction(rmB);

        QAction *editB = new QAction(GuiFactory::instance().editIcon(), trEditConnection, savebar);
        VERIFY(connect(editB, static_cast<trig>(&QAction::triggered), this, &ConnectionsDialog::edit));
        savebar->addAction(editB);

        QVBoxLayout *firstColumnLayout = new QVBoxLayout;
        firstColumnLayout->addWidget(savebar);
        firstColumnLayout->addWidget(listWidget_);
        firstColumnLayout->addLayout(bottomLayout);

        QHBoxLayout *mainLayout = new QHBoxLayout(this);
        mainLayout->addLayout(firstColumnLayout, 1);

        // Populate list with connections
        SettingsManager::ConnectionSettingsContainerType connections = SettingsManager::instance().connections();
        for (SettingsManager::ConnectionSettingsContainerType::const_iterator it = connections.begin(); it != connections.end(); ++it) {
            IConnectionSettingsBaseSPtr connectionModel = (*it);
            addConnection(connectionModel);
        }

        SettingsManager::ClusterSettingsContainerType clusters = SettingsManager::instance().clusters();
        for (SettingsManager::ClusterSettingsContainerType::const_iterator it = clusters.begin(); it != clusters.end(); ++it) {
            IClusterSettingsBaseSPtr connectionModel = (*it);
            addCluster(connectionModel);
        }

        // Highlight first item
        if (listWidget_->topLevelItemCount() > 0)
            listWidget_->setCurrentItem(listWidget_->topLevelItem(0));
        retranslateUi();
    }