Advertisement
SethVan

addRoom

May 18th, 2023
938
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.33 KB | Source Code | 0 0
  1. #include <a.out.h>
  2.  
  3. #include <QDebug>
  4. #include <QInputDialog>
  5. #include <QMessageBox>
  6. #include <QString>
  7. #include <QStringList>
  8. #include <algorithm>
  9.  
  10. #include "./ui_widget.h"
  11. #include "widget.h"
  12.  
  13. Widget::Widget( QWidget *parent ) : QWidget( parent ), ui( new Ui::Widget )
  14. {
  15.     ui->setupUi( this );
  16. }
  17.  
  18. Widget::~Widget()
  19. {
  20.     delete ui;
  21. }
  22.  
  23. void Widget::on_addItemButton_clicked()
  24. {
  25.     addNewRoom();
  26. }
  27.  
  28. void Widget::on_newItemLineEdit_returnPressed()
  29. {
  30.     addNewRoom();
  31. }
  32.  
  33. void Widget::on_deleteItemButton_clicked()
  34. {
  35.     if ( ui->listWidget->currentItem() )
  36.     {
  37.        QString room = ui->listWidget->currentItem()->text();
  38.        QString str = "Are you sure you want to permanently delete this room?:\n\"" + room + "\"";
  39.  
  40.        if ( QMessageBox::critical( this, "Deleting room is permanent!", str, QMessageBox::Yes, QMessageBox::Cancel ) ==
  41.             QMessageBox::Yes )
  42.        {
  43.           delete ui->listWidget->takeItem( ui->listWidget->currentRow() );
  44.        }
  45.     }
  46. }
  47.  
  48. void Widget::addNewRoom()
  49. {
  50.     if ( ui->newItemLineEdit->text().size() )
  51.     {
  52.         QString room = ui->newItemLineEdit->text();
  53.  
  54.         auto lastDoubleDotPos = room.lastIndexOf("..");
  55.  
  56.         /* Inside isNotAPossibleSequenceInput() all non-sequence inputs will be added normally and the
  57.          * remainder of addNewRoom() becomes irrelevant. In likewise manner valid sequential inputs are
  58.          * added inside of functions processedAsNumberSequence() and processedAsLetterSequence(). */
  59.         if ( isNotAPossibleSequenceInput( lastDoubleDotPos, room ))
  60.         {
  61.            return;
  62.         }
  63.  
  64.         QString seqBegin = room.first(lastDoubleDotPos); //left side of ".."
  65.         QString seqEnd = room.mid(lastDoubleDotPos + 2); //right side of ".."
  66.  
  67.  
  68.         if ( seqEndFormatIsInvalid(seqBegin, seqEnd, room) )
  69.         {
  70.             return;
  71.         }
  72.  
  73.         if( processedAsNumberSequence(seqBegin, seqEnd, room) )
  74.         {
  75.             return;
  76.         }
  77.  
  78.         if( processedAsLetterSequence(seqBegin, seqEnd, room) )
  79.         {
  80.             return;
  81.         }
  82.         else
  83.         {
  84.             return ambiguousFormatWarning(room);
  85.         }
  86.     }
  87. }
  88.  
  89. bool Widget::isNotAPossibleSequenceInput(int pos, const QString& room)
  90. {
  91.     if ( pos == -1 )
  92.     {
  93.         if(room.contains("."))
  94.         {
  95.             ambiguousFormatWarning(room);
  96.             return true;
  97.         }
  98.         addLineEditText();
  99.         return true;
  100.     }
  101.     if (room.lastIndexOf("...") == pos - 1 || pos == room.size() - 2 || pos == 0)
  102.     {
  103.         ambiguousFormatWarning(room);
  104.         return true;
  105.     }
  106.     return false;
  107. }
  108.  
  109. void Widget::ambiguousFormatWarning(const QString& room)
  110. {
  111.     QString ambigMsgBoxTitle = "Ambiguous format detected...";
  112.     QString ambigMsgBoxMsg = "Ambiguous format detected in inputted room:\n\"" + room +
  113.             "\"\nIf you meant to convey a sequence of inputs, then click \'Cancel\'"
  114.             " and resubmit placing at the end either number(s) or a letter (of matching case) on either side of "
  115.             "two dots with the greater value on the right side. Examples:\n\'Suite 100..120\', \'Bungalow 2A..C\'\n"
  116.             "Otherwise click \'Ignore\' and just one room will be submitted as what was written.";
  117.  
  118.     if (QMessageBox::warning( this, ambigMsgBoxTitle, ambigMsgBoxMsg, QMessageBox::Ignore, QMessageBox::Cancel )
  119.             == QMessageBox::Cancel )
  120.     {
  121.        return;
  122.     }
  123.     else
  124.     {
  125.          addLineEditText();
  126.          return;
  127.     }
  128. }
  129.  
  130. void Widget::addLineEditText()
  131. {
  132.    ui->listWidget->addItem( ui->newItemLineEdit->text() );
  133.    ui->newItemLineEdit->clear();
  134. }
  135.  
  136. bool Widget::seqEndFormatIsInvalid(const QString& seqBegin, const QString& seqEnd, const QString& room)
  137. {
  138.     if ( seqEnd.size() > 1 )
  139.     {
  140.         if (std::any_of( seqEnd.begin(), seqEnd.end(), []( auto c ) { return !c.isDigit(); } ) ||
  141.               !seqBegin.back().isDigit() )
  142.         {
  143.             ambiguousFormatWarning(room);
  144.             return true;
  145.         }
  146.         return false;
  147.     }
  148. }
  149.  
  150. bool Widget::processedAsNumberSequence(const QString& seqBegin, const QString& seqEnd, const QString& room)
  151. {
  152.     if ( seqEnd.front().isDigit() )
  153.     {
  154.         if ( std::all_of( seqBegin.begin(), seqBegin.end(), []( auto c ) { return c.isDigit(); } ) )
  155.         {
  156.             if (seqBegin.toInt() >= seqEnd.toInt())
  157.             {
  158.                 ambiguousFormatWarning(room);
  159.             }
  160.             else
  161.             {
  162.                 addLineEditSequence("", seqBegin.toInt(), seqEnd.toInt(), RoomSequence::Number);
  163.             }
  164.         }
  165.         else
  166.         {
  167.             int i = seqBegin.size() - 1;
  168.             for ( ; seqBegin[ i ].isDigit() && i > -1; --i );
  169.             auto startNumber = seqBegin.last( seqBegin.size() - (i + 1)).toInt();
  170.             auto endNumber = seqEnd.toInt();
  171.             if(startNumber < endNumber)
  172.             {
  173.                 addLineEditSequence(seqBegin.first( i + 1 ), startNumber,
  174.                                            endNumber, RoomSequence::Number );
  175.             }
  176.             else
  177.             {
  178.                 ambiguousFormatWarning(room);
  179.             }
  180.         }
  181.         return true;
  182.     }
  183.     return false;
  184. }
  185.  
  186. void Widget::addLineEditSequence(const QString& base, int roomNumberStart, int roomNumberEnd, RoomSequence type)
  187. {
  188.     int currentRoom = roomNumberStart;
  189.     for ( ; currentRoom < roomNumberEnd + 1; ++currentRoom )
  190.     {
  191.         QString addMe;
  192.         if(type == RoomSequence::Letter)
  193.         {
  194.             addMe = base + QChar(currentRoom);
  195.         }
  196.         else
  197.         {
  198.             addMe = base + QString::number(currentRoom);
  199.         }
  200.         ui->listWidget->addItem( addMe );
  201.     }
  202.     ui->newItemLineEdit->clear();
  203.  
  204. }
  205.  
  206. bool Widget::processedAsLetterSequence(const QString& seqBegin, const QString& seqEnd, const QString& room)
  207. {
  208.     if ( seqEnd.front().isLetter() && seqBegin.back().isLetter() && seqBegin.back() < seqEnd.front() &&
  209.          seqBegin.back().category() == seqEnd.front().category() ) //category refers to case
  210.     {
  211.         QString base = seqBegin.size() > 1? seqBegin.first(seqBegin.size() - 1) : "";
  212.         addLineEditSequence(base, seqBegin.back().unicode(), seqEnd.front().unicode(), RoomSequence::Letter);
  213.         return true;
  214.     }
  215.     else
  216.     {
  217.         return false;
  218.     }
  219. }
  220.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement