/*!
 * \param pParent
 */
AttachToProcessDialog::AttachToProcessDialog(QWidget *pParent)
  : QDialog(pParent)
{
  setWindowTitle(QString(Helper::applicationName).append(" - ").append(Helper::attachToRunningProcess));
  setAttribute(Qt::WA_DeleteOnClose);
  resize(500, 400);
  // attach to process id
  mpAttachToProcessIDLabel = new Label(tr("Attach to Process ID:"));
  mpAttachToProcessIDTextBox = new QLineEdit;
  // filter
  mpFilterProcessesTextBox = new QLineEdit;
  mpFilterProcessesTextBox->setPlaceholderText(tr("Filter Processes"));
  // processes tree view model & proxy
  mpProcessListModel = new ProcessListModel;
  mProcessListFilterModel.setSourceModel(mpProcessListModel);
  mProcessListFilterModel.setFilterRegExp(mpFilterProcessesTextBox->text());
  // processes tree view
  mpProcessesTreeView = new QTreeView;
  mpProcessesTreeView->setItemDelegate(new ItemDelegate(mpProcessesTreeView));
  mpProcessesTreeView->setModel(&mProcessListFilterModel);
  mpProcessesTreeView->setTextElideMode(Qt::ElideMiddle);
  mpProcessesTreeView->setIndentation(0);
  mpProcessesTreeView->setSelectionBehavior(QAbstractItemView::SelectRows);
  mpProcessesTreeView->setSelectionMode(QAbstractItemView::SingleSelection);
  mpProcessesTreeView->setUniformRowHeights(true);
  //mpProcessesTreeView->setRootIsDecorated(false);
  mpProcessesTreeView->setSortingEnabled(true);
  mpProcessesTreeView->header()->setDefaultSectionSize(100);
  mpProcessesTreeView->header()->setStretchLastSection(true);
  mpProcessesTreeView->sortByColumn(1, Qt::AscendingOrder);
  // Create the buttons
  mpOkButton = new QPushButton(Helper::ok);
  mpOkButton->setEnabled(false);
  connect(mpOkButton, SIGNAL(clicked()), SLOT(attachProcess()));
  mpRefreshButton = new QPushButton(Helper::refresh);
  connect(mpRefreshButton, SIGNAL(clicked()), SLOT(updateProcessList()));
  mpCancelButton = new QPushButton(Helper::cancel);
  connect(mpCancelButton, SIGNAL(clicked()), SLOT(reject()));
  // create buttons box
  mpButtonBox = new QDialogButtonBox(Qt::Horizontal);
  mpButtonBox->addButton(mpOkButton, QDialogButtonBox::ActionRole);
  mpButtonBox->addButton(mpRefreshButton, QDialogButtonBox::ActionRole);
  mpButtonBox->addButton(mpCancelButton, QDialogButtonBox::ActionRole);

  connect(mpAttachToProcessIDTextBox, SIGNAL(textChanged(QString)), this, SLOT(processIDChanged(QString)));
  connect(mpFilterProcessesTextBox, SIGNAL(textChanged(QString)), this, SLOT(setFilterString(QString)));
  connect(mpProcessesTreeView, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(processSelected(QModelIndex)));
  connect(mpProcessesTreeView, SIGNAL(clicked(QModelIndex)), this, SLOT(processClicked(QModelIndex)));
  // set the layout
  QGridLayout *pMainLayout = new QGridLayout;
  pMainLayout->setAlignment(Qt::AlignTop | Qt::AlignLeft);
  pMainLayout->addWidget(mpAttachToProcessIDLabel, 0, 0);
  pMainLayout->addWidget(mpAttachToProcessIDTextBox, 0, 1);
  pMainLayout->addWidget(mpFilterProcessesTextBox, 1, 0, 1 ,2);
  pMainLayout->addWidget(mpProcessesTreeView, 2, 0, 1 ,2);
  pMainLayout->addWidget(mpButtonBox, 3, 0, 1, 2, Qt::AlignRight);
  setLayout(pMainLayout);
  // get the list of processes
  updateProcessList();
}
void SelectProcess::on_pushButton_Ok_clicked()
{
    if (m_PID > 0) {
        emit processSelected(m_PID);
        close();
    } else {
        addLogErr(trUtf8("Сначала выберите процесс или отмените выбор!"));
        QMessageBox::information(this, trUtf8("Информация"), trUtf8("Сначала выберите процесс или отмените выбор!"));
    }
}