File indexing completed on 2024-04-28 04:38:54

0001 /*
0002     SPDX-FileCopyrightText: 2010 Julien Desgats <julien.desgats@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "greputil.h"
0008 
0009 #include <algorithm>
0010 #include <QChar>
0011 #include <QComboBox>
0012 
0013 static int const MAX_LAST_SEARCH_ITEMS_COUNT = 15;
0014 
0015 QString substitudePattern(const QString& pattern, const QString& searchString)
0016 {
0017     QString subst = searchString;
0018     QString result;
0019     bool expectEscape = false;
0020     for (const QChar ch : pattern) {
0021         if(expectEscape)
0022         {
0023             expectEscape = false;
0024             if (ch == QLatin1Char('%'))
0025                 result.append(QLatin1Char('%'));
0026             else if (ch == QLatin1Char('s'))
0027                 result.append(subst);
0028             else
0029                 result.append(QLatin1Char('%') + ch);
0030         }
0031         else if (ch == QLatin1Char('%'))
0032             expectEscape = true;
0033         else
0034             result.append(ch);
0035     }
0036     return result;
0037 }
0038 
0039 QStringList qCombo2StringList( QComboBox* combo, bool allowEmpty )
0040 {
0041     QStringList list;
0042     if (!combo) {
0043         return list;
0044     }
0045     QString currentText = combo->currentText();
0046     int skippedItem = combo->currentIndex();
0047     if (!currentText.isEmpty() || allowEmpty) {
0048         list << currentText;
0049     }
0050     if (skippedItem != -1 && currentText != combo->itemText(skippedItem)) {
0051         skippedItem = -1;
0052     }
0053     for (int i = 0; i < std::min(MAX_LAST_SEARCH_ITEMS_COUNT, combo->count()); ++i) {
0054         if (i != skippedItem && !combo->itemText(i).isEmpty()) {
0055             list << combo->itemText(i);
0056         }
0057     }
0058     return list;
0059 }
0060