Advertisement
SethVan

addRoom2

May 18th, 2023 (edited)
1,150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.43 KB | Source Code | 0 0
  1. #include <a.out.h>
  2. #include <sqlite3.h>
  3. #include <QDebug>
  4. #include <QInputDialog>
  5. #include <QMessageBox>
  6. #include <QString>
  7. #include <QStringList>
  8. #include <QRegularExpression>
  9. #include <QRegularExpressionMatch>
  10. #include <algorithm>
  11.  
  12. #include "./ui_widget.h"
  13. #include "widget.h"
  14.  
  15. Widget::Widget( QWidget *parent ) : QWidget( parent ), ui( new Ui::Widget )
  16. {
  17.     ui->setupUi( this );
  18. }
  19.  
  20. Widget::~Widget()
  21. {
  22.     delete ui;
  23. }
  24.  
  25. void Widget::on_addItemButton_clicked()
  26. {
  27.     addNewRoom();
  28. }
  29.  
  30. void Widget::on_newItemLineEdit_returnPressed()
  31. {
  32.     addNewRoom();
  33. }
  34.  
  35. void Widget::on_deleteItemButton_clicked()
  36. {
  37.     if ( ui->listWidget->currentItem() )
  38.     {
  39.        QString room = ui->listWidget->currentItem()->text();
  40.        QString str = "Are you sure you want to permanently delete this room?:\n\"" + room + "\"";
  41.  
  42.        if ( QMessageBox::critical( this, "Deleting room is permanent!", str, QMessageBox::Yes, QMessageBox::Cancel ) ==
  43.             QMessageBox::Yes )
  44.        {
  45.           delete ui->listWidget->takeItem( ui->listWidget->currentRow() );
  46.        }
  47.     }
  48. }
  49.  
  50. void Widget::addNewRoom()
  51. {
  52.     if ( ui->newItemLineEdit->text().size() )
  53.     {
  54.         QString room = ui->newItemLineEdit->text();
  55.  
  56.         auto lastDoubleDotPos = room.lastIndexOf("..");
  57.  
  58.         // Inside isNotAPossibleSequenceInput() all non-sequence inputs will be added normally
  59.         if ( isNotAPossibleSequenceInput( lastDoubleDotPos, room ))
  60.         {
  61.            return;
  62.         }
  63.  
  64.         static const QRegularExpression upperLetterSequence("^(?<base>.*)(?<start>[A-Z])\\.\\.(?<end>[A-Z])\\s*$");
  65.         if ( upperLetterSequence.match(room).hasMatch() )
  66.         {
  67.             auto match = upperLetterSequence.match(room);
  68.             addLineEditSequence(match.captured(1), match.captured(2).front().unicode(), match.captured(3).front().unicode(), RoomSequence::Letter);
  69.             return;
  70.         }
  71.         static const QRegularExpression lowerLetterSequence("^(?<base>.*)(?<start>[a-z])\\.\\.(?<end>[a-z])\\s*$");
  72.         if ( lowerLetterSequence.match(room).hasMatch() )
  73.         {
  74.             auto match = lowerLetterSequence.match(room);
  75.             addLineEditSequence(match.captured(1), match.captured(2).front().unicode(), match.captured(3).front().unicode(), RoomSequence::Letter);
  76.             return;
  77.         }
  78.         static const QRegularExpression baseToNumberSequence("^(?<base>.*\\D+)(?<start>[1-9]\\d*)\\.\\.(?<end>[1-9]\\d*)\\s*$");
  79.         if( baseToNumberSequence.match(room).hasMatch())
  80.         {
  81.             auto match = baseToNumberSequence.match(room);
  82.             addLineEditSequence(match.captured(1), match.captured(2).toInt(), match.captured(3).toInt(), RoomSequence::Number);
  83.             qDebug() << "\"" << match.captured(1) << "\", \"" << match.captured(2).toInt() << "\", \""
  84.                                  << match.captured(3).toInt() << "\"";
  85.             return;
  86.         }
  87.         static const QRegularExpression pureNumberSequence("^(?<start>[1-9]\\d*)\\.\\.(?<end>[1-9]\\d*)\\s*$");
  88.         if( pureNumberSequence.match(room).hasMatch())
  89.         {
  90.             auto match = pureNumberSequence.match(room);
  91.             addLineEditSequence("", match.captured(1).toInt(), match.captured(2).toInt(), RoomSequence::Number);
  92. //            qDebug() << "\"" << match.captured(1) << "\", \"" << match.captured(2).toInt() << "\", \""
  93. //                                 << match.captured(3).toInt() << "\"";
  94.             return;
  95.         }
  96.         else
  97.         {
  98.             return ambiguousFormatWarning(room);
  99.         }
  100.     }
  101. }
  102.  
  103. bool Widget::isNotAPossibleSequenceInput(int pos, const QString& room)
  104. {
  105.     if ( pos == -1 )
  106.     {
  107.         if(room.contains("."))
  108.         {
  109.             ambiguousFormatWarning(room);
  110.             return true;
  111.         }
  112.         addLineEditText();
  113.         return true;
  114.     }
  115.     if (room.lastIndexOf("...") == pos - 1 || pos == room.size() - 2 || pos == 0)
  116.     {
  117.         ambiguousFormatWarning(room);
  118.         return true;
  119.     }
  120.     return false;
  121. }
  122.  
  123. void Widget::ambiguousFormatWarning(const QString& room)
  124. {
  125.     QString ambigMsgBoxTitle = "Ambiguous format detected...";
  126.     QString ambigMsgBoxMsg = "Ambiguous format detected in inputted room:\n\"" + room +
  127.             "\"\nIf you meant to convey a sequence of inputs, then click \'Cancel\'"
  128.             " and resubmit placing at the end either number(s) or a letter (of matching case) on either side of "
  129.             "two dots with the greater value on the right side. Examples:\n\'Suite 100..120\', \'Bungalow 2A..C\'\n"
  130.             "Otherwise click \'Ignore\' and just one room will be submitted as what was written.";
  131.  
  132.     if (QMessageBox::warning( this, ambigMsgBoxTitle, ambigMsgBoxMsg, QMessageBox::Ignore, QMessageBox::Cancel )
  133.             == QMessageBox::Cancel )
  134.     {
  135.        return;
  136.     }
  137.     else
  138.     {
  139.          addLineEditText();
  140.          return;
  141.     }
  142. }
  143.  
  144. void Widget::addLineEditText()
  145. {
  146.     QString addMe = ui->newItemLineEdit->text().trimmed();
  147.     auto existingItems = ui->listWidget->findItems(addMe, Qt::MatchExactly);
  148.     if (existingItems.isEmpty()) {
  149.         // Add the item to the list only if it doesn't exist already
  150.         ui->listWidget->addItem(addMe);
  151.         ui->newItemLineEdit->clear();
  152.     }
  153.     else
  154.     {
  155.         QString msg = "Room " + addMe + " could not be added as it already exists.";
  156.         QMessageBox::information(this, "Could not add room", msg);
  157.     }
  158.  
  159.  
  160. }
  161.  
  162. void Widget::addLineEditSequence(const QString& base, int roomNumberStart, int roomNumberEnd, RoomSequence type)
  163. {
  164.  
  165.     for ( int currentRoom = roomNumberStart; currentRoom < roomNumberEnd + 1; ++currentRoom )
  166.     {
  167.         QString addMe;
  168.         if(type == RoomSequence::Letter)
  169.         {
  170.             addMe = base + QChar(currentRoom);
  171.         }
  172.         else
  173.         {
  174.             addMe = base + QString::number(currentRoom);
  175.         }
  176.         auto trimmed = addMe.trimmed();
  177.         auto existingItems = ui->listWidget->findItems(trimmed, Qt::MatchExactly);
  178.         if (existingItems.isEmpty()) {
  179.             // Add the item to the list only if it doesn't exist already
  180.             ui->listWidget->addItem(addMe);
  181.             ui->newItemLineEdit->clear();
  182.         }
  183.         else
  184.         {
  185.             QString msg = "Room " + trimmed + " could not be added as it already exists.";
  186.             QMessageBox::information(this, "Could not add room", msg);
  187.         }
  188.  
  189.     }
  190.     ui->newItemLineEdit->clear();
  191.  
  192. }
  193.  
  194.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement