Skip to content

Commit c4f5aa1

Browse files
committed
Style per column!
1 parent 8feabf8 commit c4f5aa1

File tree

1 file changed

+45
-7
lines changed

1 file changed

+45
-7
lines changed

src/openstudio_lib/ScheduleFileInspectorView.cpp

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141

4242
#include <openstudio/utilities/core/Assert.hpp>
4343
#include <openstudio/utilities/core/Filesystem.hpp>
44+
#include <openstudio/utilities/core/Compare.hpp>
4445

4546
#include <QGridLayout>
4647
#include <QLabel>
@@ -50,6 +51,7 @@
5051
#include <QTextStream>
5152
#include <QPlainTextEdit>
5253
#include <QPushButton>
54+
#include <Qt> // Qt namespace, for SplitBehaviorFlags
5355

5456
namespace openstudio {
5557

@@ -310,7 +312,14 @@ void ScheduleFileInspectorView::attach(openstudio::model::ScheduleFile& sch) {
310312

311313
// OSIntegerEdit2
312314
m_columnNumber->bind(*m_sch, IntGetter(std::bind(&model::ScheduleFile::columnNumber, m_sch.get_ptr())),
313-
boost::optional<IntSetter>(std::bind(&model::ScheduleFile::setColumnNumber, m_sch.get_ptr(), std::placeholders::_1)));
315+
boost::optional<IntSetter>([this](int value) -> bool {
316+
bool result = m_sch->setColumnNumber(value);
317+
if (result) {
318+
refreshContent();
319+
refreshError();
320+
}
321+
return result;
322+
}));
314323

315324
m_rowstoSkipatTop->bind(*m_sch, IntGetter(std::bind(&model::ScheduleFile::rowstoSkipatTop, m_sch.get_ptr())),
316325
boost::optional<IntSetter>([this](int value) -> bool {
@@ -433,6 +442,21 @@ void ScheduleFileInspectorView::refreshContent() {
433442
if (openstudio::filesystem::is_regular_file(fpath)) {
434443
const int rowstoSkipatTop = m_sch->rowstoSkipatTop();
435444

445+
const int colNum = m_sch->columnNumber() - 1; // Turn 1-indexed to 0-indexed
446+
447+
QString sep;
448+
Qt::SplitBehavior behavior = Qt::KeepEmptyParts;
449+
if (openstudio::istringEqual(m_sch->columnSeparator(), "Comma")) {
450+
sep = ',';
451+
} else if (openstudio::istringEqual(m_sch->columnSeparator(), "Tab")) {
452+
sep = '\t';
453+
} else if (openstudio::istringEqual(m_sch->columnSeparator(), "Space")) {
454+
sep = ' '; // QRegularExpression("\\s+")
455+
behavior = Qt::SkipEmptyParts;
456+
} else if (openstudio::istringEqual(m_sch->columnSeparator(), "Semicolon")) {
457+
sep = ';';
458+
} // Fixed: not handled
459+
436460
if (m_lines.isEmpty()) {
437461
m_lines.clear();
438462

@@ -453,20 +477,34 @@ void ScheduleFileInspectorView::refreshContent() {
453477

454478
const int read_n_lines = std::min(std::max(rowstoSkipatTop + 2, 10), static_cast<int>(m_lines.size()));
455479

480+
auto constructStyledLine = [&sep, &behavior, &colNum](const QString& line, const QString& color) {
481+
QStringList vals = line.split(sep, behavior);
482+
QString str;
483+
for (int j = 0; j < vals.size(); ++j) {
484+
QString val = vals[j];
485+
if (j == colNum) {
486+
str.append(QString("<span style='color: %1'>%2</span>").arg(color).arg(val));
487+
} else {
488+
str.append(QString("<span style='color: gray'>%1</span>").arg(val));
489+
}
490+
if (j < vals.size() - 1) {
491+
str.append(",");
492+
}
493+
}
494+
return str;
495+
};
496+
456497
int curLine = 0;
457498
for (const QString& line : m_lines) {
458499
if (curLine < rowstoSkipatTop) {
459-
m_contentLines->appendHtml(QString("<span style='color: gray'>%1</span>").arg(line));
460-
} else if (m_displayAllContent || (curLine < read_n_lines)) {
461-
m_contentLines->appendHtml(QString("<span style='color: green'>%1</span>").arg(line));
500+
m_contentLines->appendHtml(constructStyledLine(line, "black"));
501+
} else if (m_displayAllContent || (curLine < read_n_lines) || (curLine > m_lines.size() - 5)) {
502+
m_contentLines->appendHtml(constructStyledLine(line, "green"));
462503
} else if (curLine == read_n_lines) {
463504
m_contentLines->appendHtml("<strong>[...]</strong>");
464-
} else if (curLine > m_lines.size() - 5) {
465-
m_contentLines->appendHtml(QString("<span style='color: green'>%1</span>").arg(line));
466505
}
467506
++curLine;
468507
}
469-
470508
} else {
471509
m_contentLines->setPlainText(QString("File not found at '%1'").arg(toQString(fpath)));
472510
}

0 commit comments

Comments
 (0)