refactor display code
diff --git a/chatdialog.cpp b/chatdialog.cpp
index 38750c6..2519089 100644
--- a/chatdialog.cpp
+++ b/chatdialog.cpp
@@ -189,10 +189,6 @@
nickFormat.setFontWeight(QFont::Bold);
nickFormat.setFontUnderline(true);
nickFormat.setUnderlineColor(Qt::gray);
- QTextCharFormat timeFormat;
- timeFormat.setForeground(Qt::gray);
- timeFormat.setFontUnderline(true);
- timeFormat.setUnderlineColor(Qt::gray);
QTextCursor cursor(textEdit->textCursor());
cursor.movePosition(QTextCursor::End);
@@ -203,30 +199,9 @@
QTextTableCell fromCell = table->cellAt(0, 0);
fromCell.setFormat(nickFormat);
fromCell.firstCursorPosition().insertText(from);
- QTextTableCell timeCell = table->cellAt(0, 1);
- timeCell.setFormat(timeFormat);
+
time_t timestamp = msg.timestamp();
- struct tm *tm_time = localtime(×tamp);
- int hour = tm_time->tm_hour;
- QString amOrPM;
- if (hour > 12)
- {
- hour -= 12;
- amOrPM = "PM";
- }
- else
- {
- amOrPM = "AM";
- if (hour == 0)
- {
- hour = 12;
- }
- }
-
- char textTime[12];
- sprintf(textTime, "%d:%02d:%02d %s", hour, tm_time->tm_min, tm_time->tm_sec, amOrPM.toStdString().c_str());
- timeCell.firstCursorPosition().insertText(textTime);
-
+ printTimeInCell(table, timestamp);
QTextCursor nextCursor(textEdit->textCursor());
nextCursor.movePosition(QTextCursor::End);
@@ -242,10 +217,6 @@
nickFormat.setFontWeight(QFont::Bold);
nickFormat.setFontUnderline(true);
nickFormat.setUnderlineColor(Qt::gray);
- QTextCharFormat timeFormat;
- timeFormat.setForeground(Qt::gray);
- timeFormat.setFontUnderline(true);
- timeFormat.setUnderlineColor(Qt::gray);
QTextCursor cursor(textEdit->textCursor());
cursor.movePosition(QTextCursor::End);
@@ -266,35 +237,52 @@
QTextTableCell fromCell = table->cellAt(0, 0);
fromCell.setFormat(nickFormat);
fromCell.firstCursorPosition().insertText(from);
- QTextTableCell timeCell = table->cellAt(0, 1);
- timeCell.setFormat(timeFormat);
- time_t timestamp = msg.timestamp();
- struct tm *tm_time = localtime(×tamp);
- int hour = tm_time->tm_hour;
- QString amOrPM;
- if (hour > 12)
- {
- hour -= 12;
- amOrPM = "PM";
- }
- else
- {
- amOrPM = "AM";
- if (hour == 0)
- {
- hour = 12;
- }
- }
- char textTime[12];
- sprintf(textTime, "%d:%02d:%02d %s", hour, tm_time->tm_min, tm_time->tm_sec, amOrPM.toStdString().c_str());
- timeCell.firstCursorPosition().insertText(textTime);
+ time_t timestamp = msg.timestamp();
+ printTimeInCell(table, timestamp);
}
QScrollBar *bar = textEdit->verticalScrollBar();
bar->setValue(bar->maximum());
}
+void
+ChatDialog::printTimeInCell(QTextTable *table, time_t timestamp)
+{
+ QTextCharFormat timeFormat;
+ timeFormat.setForeground(Qt::gray);
+ timeFormat.setFontUnderline(true);
+ timeFormat.setUnderlineColor(Qt::gray);
+ QTextTableCell timeCell = table->cellAt(0, 1);
+ timeCell.setFormat(timeFormat);
+ timeCell.firstCursorPosition().insertText(formatTime(timestamp));
+}
+
+QString
+ChatDialog::formatTime(time_t timestamp)
+{
+ struct tm *tm_time = localtime(×tamp);
+ int hour = tm_time->tm_hour;
+ QString amOrPM;
+ if (hour > 12)
+ {
+ hour -= 12;
+ amOrPM = "PM";
+ }
+ else
+ {
+ amOrPM = "AM";
+ if (hour == 0)
+ {
+ hour = 12;
+ }
+ }
+
+ char textTime[12];
+ sprintf(textTime, "%d:%02d:%02d %s", hour, tm_time->tm_min, tm_time->tm_sec, amOrPM.toStdString().c_str());
+ return QString(textTime);
+}
+
void
ChatDialog::processTreeUpdateWrapper(const std::vector<Sync::MissingDataInfo> v, Sync::SyncAppSocket *sock)
{
diff --git a/chatdialog.h b/chatdialog.h
index bfc1a33..1ee4216 100644
--- a/chatdialog.h
+++ b/chatdialog.h
@@ -17,6 +17,7 @@
class QAction;
class QMenu;
class QStringListModel;
+class QTextTable;
class ChatDialog : public QDialog, private Ui::ChatDialog
{
@@ -55,6 +56,8 @@
void testDraw();
void createTrayIcon();
void createActions();
+ QString formatTime(time_t);
+ void printTimeInCell(QTextTable *, time_t);
private slots:
void returnPressed();