Add brackets to timestamp when copying if hidden
authorShane Synan <digitalcircuit36939@gmail.com>
Tue, 28 Jun 2016 02:33:35 +0000 (22:33 -0400)
committerManuel Nickschas <sputnick@quassel-irc.org>
Tue, 6 Sep 2016 20:14:48 +0000 (22:14 +0200)
Add brackets around timestamp if ShowSenderBrackets is disabled -and-
the timestamp format does not contain brackets.  This matches all
common types of brackets using a regular expression.

Remove any leading and trailing space from timestamp format before
copying if above conditions met, to avoid having [ 11:04:32] or some
such.  This might be unexpected, but if someone has specific needs
for the bracket arrangement, they can toggle to always show sender
brackets.

Examples with show sender brackets disabled:
" hh:mm:ss"     -> "[hh:mm:ss]"
" h:mm:ss ap"   -> "[h:mm:ss ap]"
"<hh mm ss"     -> "[<hh mm ss]"
"[hh:mm:ss]"    -> unchanged
"<hh mm ss>"    -> unchanged
"{hh:mm:ss:ss>" -> unchanged (and I won't ask why you're doing this)

For regular expression debugging and explanations,
See: https://regex101.com/

src/qtui/chatscene.cpp
src/qtui/chatscene.h

index 9654626..188eba6 100644 (file)
@@ -134,6 +134,10 @@ ChatScene::ChatScene(QAbstractItemModel *model, const QString &idString, qreal w
     _showSenderBrackets = defaultSettings.showSenderBrackets();
     defaultSettings.notify("ShowSenderBrackets", this, SLOT(showSenderBracketsChanged()));
 
     _showSenderBrackets = defaultSettings.showSenderBrackets();
     defaultSettings.notify("ShowSenderBrackets", this, SLOT(showSenderBracketsChanged()));
 
+    _timestampFormatString = defaultSettings.timestampFormatString();
+    defaultSettings.notify("TimestampFormat", this, SLOT(timestampFormatStringChanged()));
+    updateTimestampHasBrackets();
+
     _clickTimer.setInterval(QApplication::doubleClickInterval());
     _clickTimer.setSingleShot(true);
     connect(&_clickTimer, SIGNAL(timeout()), SLOT(clickTimeout()));
     _clickTimer.setInterval(QApplication::doubleClickInterval());
     _clickTimer.setSingleShot(true);
     connect(&_clickTimer, SIGNAL(timeout()), SLOT(clickTimeout()));
@@ -1026,9 +1030,21 @@ QString ChatScene::selection() const
             return QString();
         }
         QString result;
             return QString();
         }
         QString result;
+
         for (int l = start; l <= end; l++) {
         for (int l = start; l <= end; l++) {
-            if (_selectionMinCol == ChatLineModel::TimestampColumn)
-                result += _lines[l]->item(ChatLineModel::TimestampColumn)->data(MessageModel::DisplayRole).toString() + " ";
+            if (_selectionMinCol == ChatLineModel::TimestampColumn) {
+                ChatItem *item = _lines[l]->item(ChatLineModel::TimestampColumn);
+                if (!_showSenderBrackets && !_timestampHasBrackets) {
+                    // Only re-add brackets if the current timestamp format does not include them
+                    // -and- sender brackets are disabled.  Don't filter on Message::Plain as
+                    // timestamp brackets affect all types of messages.
+                    // Remove any spaces before and after, otherwise it may look weird.
+                    result += QString("[%1] ").arg(item->data(MessageModel::DisplayRole)
+                                                   .toString().trimmed());
+                } else {
+                    result += item->data(MessageModel::DisplayRole).toString() + " ";
+                }
+            }
             if (_selectionMinCol <= ChatLineModel::SenderColumn) {
                 ChatItem *item = _lines[l]->item(ChatLineModel::SenderColumn);
                 if (!_showSenderBrackets && item->chatLine()->msgType() == Message::Plain) {
             if (_selectionMinCol <= ChatLineModel::SenderColumn) {
                 ChatItem *item = _lines[l]->item(ChatLineModel::SenderColumn);
                 if (!_showSenderBrackets && item->chatLine()->msgType() == Message::Plain) {
@@ -1329,3 +1345,35 @@ void ChatScene::showSenderBracketsChanged()
     ChatViewSettings settings;
     _showSenderBrackets = settings.showSenderBrackets();
 }
     ChatViewSettings settings;
     _showSenderBrackets = settings.showSenderBrackets();
 }
+
+void ChatScene::timestampFormatStringChanged()
+{
+    ChatViewSettings settings;
+    _timestampFormatString = settings.timestampFormatString();
+    updateTimestampHasBrackets();
+}
+
+void ChatScene::updateTimestampHasBrackets()
+{
+    // Calculate these parameters only as needed, rather than on-demand
+
+    // Does the timestamp format contain brackets?  For example:
+    // Classic: "[hh:mm:ss]"
+    // Modern:  " hh:mm:ss"
+    //
+    // Match groups of any opening or closing brackets - (), {}, [], <>, (>, {], etc:
+    //   ^\s*[({[<].+[)}\]>]\s*$
+    //   [...]    is a character group containing ...
+    //   ^        matches start of string
+    //   \s*      matches any amount of whitespace
+    //   [({[<]   matches (, {, [, or <
+    //   .+       matches one or more characters
+    //   [)}\]>]  matches ), }, ], or >, escaping the ]
+    //   $        matches end of string
+    // Alternatively, if opening and closing brackets must be in pairs, use this:
+    //   (^\s*\(.+\)\s*$)|(^\s*\{.+\}\s*$)|(^\s*\[.+\]\s*$)|(^\s*<.+>\s*$)
+    // Note that '\' must be escaped as '\\'
+    // Helpful interactive website for debugging and explaining:  https://regex101.com/
+    const QRegExp regExpMatchBrackets("^\\s*[({[<].+[)}\\]>]\\s*$");
+    _timestampHasBrackets = regExpMatchBrackets.exactMatch(_timestampFormatString);
+}
index 68ef20f..e06d22d 100644 (file)
@@ -185,6 +185,20 @@ private slots:
      */
     void showSenderBracketsChanged();
 
      */
     void showSenderBracketsChanged();
 
+    /**
+     * Updates the local setting cache of the timestamp format string
+     */
+    void timestampFormatStringChanged();
+
+    /**
+     * Updates the status of whether or not the timestamp format string contains brackets
+     *
+     * When the timestamp contains brackets -and- showSenderBrackets is disabled, we need to
+     * automatically add brackets.  This function checks if the timestamp has brackets and stores
+     * the result, rather than checking each time text is copied.
+     */
+    void updateTimestampHasBrackets();
+
     void rowsRemoved();
 
     void clickTimeout();
     void rowsRemoved();
 
     void clickTimeout();
@@ -233,6 +247,9 @@ private:
 
     bool _showSenderBrackets;  /// If true, show brackets around sender names
 
 
     bool _showSenderBrackets;  /// If true, show brackets around sender names
 
+    QString _timestampFormatString; /// Format of the timestamp string
+    bool _timestampHasBrackets;     /// If true, timestamp format has [brackets] of some sort
+
     static const int _webSearchSelectionTextMaxVisible = 24;
 
 #if defined HAVE_WEBKIT || defined HAVE_WEBENGINE
     static const int _webSearchSelectionTextMaxVisible = 24;
 
 #if defined HAVE_WEBKIT || defined HAVE_WEBENGINE