consolewindow.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #include "consolewindow.h"
  2. #include "ui_consolewindow.h"
  3. ConsoleWindow::ConsoleWindow(QWidget *parent) :
  4. QMainWindow(parent),
  5. ui(new Ui::ConsoleWindow),
  6. findNext(false)
  7. {
  8. ui->setupUi(this);
  9. sourceModel = new ConsoleModel(this);
  10. model = new SortFilterProxyModel(this);
  11. model->setSourceModel(sourceModel);
  12. model->setFilterKeyColumn(0);
  13. ui->consoleListView->setModel(model);
  14. ui->consoleListView->setColumnWidth(0, 2000);
  15. connect(sourceModel, SIGNAL(beforeInserting()),
  16. this, SLOT(setConditionalScroll()));
  17. connect(sourceModel, SIGNAL(afterInserting()),
  18. this, SLOT(conditionalScroll()));
  19. connect(ui->btnSend, SIGNAL(clicked()),
  20. this, SLOT(cmdSendClicked()));
  21. connect(ui->lineCmd, SIGNAL(textChanged(QString)),
  22. this, SLOT(lineCmdChanged(QString)));
  23. _levelFilter = new QSignalMapper(this);
  24. connect(ui->checkEmerg, SIGNAL(clicked()), _levelFilter, SLOT(map()));
  25. connect(ui->checkAlert, SIGNAL(clicked()), _levelFilter, SLOT(map()));
  26. connect(ui->checkCrit, SIGNAL(clicked()), _levelFilter, SLOT(map()));
  27. connect(ui->checkDebug, SIGNAL(clicked()), _levelFilter, SLOT(map()));
  28. connect(ui->checkError, SIGNAL(clicked()), _levelFilter, SLOT(map()));
  29. connect(ui->checkInfo, SIGNAL(clicked()), _levelFilter, SLOT(map()));
  30. connect(ui->checkNotice, SIGNAL(clicked()), _levelFilter, SLOT(map()));
  31. connect(ui->checkWarn, SIGNAL(clicked()), _levelFilter, SLOT(map()));
  32. _levelFilter->setMapping(ui->checkEmerg, SWITCH_LOG_CONSOLE);
  33. _levelFilter->setMapping(ui->checkAlert, SWITCH_LOG_ALERT);
  34. _levelFilter->setMapping(ui->checkCrit, SWITCH_LOG_CRIT);
  35. _levelFilter->setMapping(ui->checkDebug, SWITCH_LOG_DEBUG);
  36. _levelFilter->setMapping(ui->checkError, SWITCH_LOG_ERROR);
  37. _levelFilter->setMapping(ui->checkInfo, SWITCH_LOG_INFO);
  38. _levelFilter->setMapping(ui->checkNotice, SWITCH_LOG_NOTICE);
  39. _levelFilter->setMapping(ui->checkWarn, SWITCH_LOG_WARNING);
  40. connect(_levelFilter, SIGNAL(mapped(int)), this, SLOT(filterModelLogLevel(int)));
  41. connect(ui->btnFilterClear, SIGNAL(clicked()),
  42. this, SLOT(filterClear()));
  43. connect(ui->lineFilter, SIGNAL(textChanged(QString)),
  44. this, SLOT(filterStringChanged()));
  45. connect(ui->filterCaseSensitivityCheckBox, SIGNAL(toggled(bool)),
  46. this, SLOT(filterStringChanged()));
  47. connect(ui->filterSyntaxComboBox, SIGNAL(currentIndexChanged(int)),
  48. this, SLOT(filterStringChanged()));
  49. connect(ui->filterReverseCheckBox, SIGNAL(toggled(bool)),
  50. this, SLOT(reverseFilterChecked()));
  51. connect(g_FSHost, SIGNAL(eventLog(QSharedPointer<switch_log_node_t>,switch_log_level_t)), this, SLOT(loggerHandler(QSharedPointer<switch_log_node_t>,switch_log_level_t)));
  52. }
  53. ConsoleWindow::~ConsoleWindow()
  54. {
  55. delete ui;
  56. }
  57. void ConsoleWindow::changeEvent(QEvent *e)
  58. {
  59. QMainWindow::changeEvent(e);
  60. switch (e->type()) {
  61. case QEvent::LanguageChange:
  62. ui->retranslateUi(this);
  63. break;
  64. default:
  65. break;
  66. }
  67. }
  68. void ConsoleWindow::setConditionalScroll()
  69. {
  70. autoScroll = (ui->consoleListView->verticalScrollBar()->maximum() == ui->consoleListView->verticalScrollBar()->value());
  71. }
  72. void ConsoleWindow::conditionalScroll()
  73. {
  74. if (autoScroll)
  75. ui->consoleListView->scrollToBottom();
  76. }
  77. void ConsoleWindow::cmdSendClicked()
  78. {
  79. if (ui->lineCmd->text().isEmpty()) return;
  80. QString cmd = ui->lineCmd->text().split(" ", QString::SkipEmptyParts)[0];
  81. if (cmd.isEmpty()) return;
  82. QStringList split = ui->lineCmd->text().split(" ", QString::SkipEmptyParts);
  83. if (split.isEmpty()) return;
  84. QString args;
  85. for (int i=1; i<split.length(); i++)
  86. {
  87. args += split[i];
  88. if (i!=split.length()-1)
  89. args += " ";
  90. }
  91. QString res;
  92. g_FSHost->sendCmd(cmd.toAscii().data(), args.toAscii().data(), &res);
  93. if (!res.isEmpty())
  94. {
  95. /* Remove \r\n */
  96. QStringList textList = res.split(QRegExp("(\r+)"), QString::SkipEmptyParts);
  97. QString final_str;
  98. for (int line = 0; line<textList.size(); line++)
  99. {
  100. final_str += textList[line];
  101. }
  102. QStringList lines = final_str.split(QRegExp("(\n+)"), QString::SkipEmptyParts);
  103. for (int line = 0; line < lines.size(); ++line)
  104. {
  105. QStandardItem *item = new QStandardItem(lines[line]);
  106. item->setData(SWITCH_LOG_CONSOLE, ConsoleModel::LogLevelRole);
  107. addNewConsoleItem(item);
  108. }
  109. }
  110. ui->lineCmd->clear();
  111. }
  112. void ConsoleWindow::lineCmdChanged(QString text)
  113. {
  114. ui->btnSend->setDisabled(text.isEmpty());
  115. }
  116. void ConsoleWindow::filterModelLogLevel(int level)
  117. {
  118. model->setLogLevelFilter(level);
  119. }
  120. void ConsoleWindow::loggerHandler(QSharedPointer<switch_log_node_t> node, switch_log_level_t level)
  121. {
  122. if (level > ui->comboLogLevel->currentIndex()) return;
  123. QString text(node.data()->data);
  124. if (!text.isEmpty())
  125. {
  126. /* Remove \r\n */
  127. QStringList textList = text.split(QRegExp("(\r+)"), QString::SkipEmptyParts);
  128. QString final_str;
  129. for (int line = 0; line<textList.size(); line++)
  130. {
  131. final_str += textList[line];
  132. }
  133. QStringList lines = final_str.split(QRegExp("(\n+)"), QString::SkipEmptyParts);
  134. for (int line = 0; line < lines.size(); ++line)
  135. {
  136. QStandardItem *item = new QStandardItem(lines[line]);
  137. item->setData(level, ConsoleModel::LogLevelRole);
  138. item->setData(node.data()->userdata, ConsoleModel::UUIDRole);
  139. addNewConsoleItem(item);
  140. }
  141. }
  142. }
  143. void ConsoleWindow::addNewConsoleItem(QStandardItem *item)
  144. {
  145. QSettings settings;
  146. settings.beginGroup("Console");
  147. QPalette palette = settings.value(QString("log-level-%1-palette").arg(item->data(Qt::UserRole).toInt())).value<QPalette>();
  148. QFont font = settings.value(QString("log-level-%1-font").arg(item->data(Qt::UserRole).toInt())).value<QFont>();
  149. item->setBackground(palette.base());
  150. item->setForeground(palette.text());
  151. item->setFont(font);
  152. sourceModel->appendRow(item);
  153. }