123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- #include "columnedit.h"
- #include "scintillaeditview.h"
- ColumnEdit::ColumnEdit(QWidget *parent)
- : QWidget(parent), m_curEditWin(nullptr), m_editTabWidget(nullptr)
- {
- ui.setupUi(this);
- connect(ui.addPrefix, &QCheckBox::stateChanged, this, &ColumnEdit::slot_addPrefix);
- connect(ui.is16, &QRadioButton::clicked, this, &ColumnEdit::slot_bigChar);
- }
- ColumnEdit::~ColumnEdit()
- {
- }
- void ColumnEdit::slot_insertTextEnable(bool check)
- {
- if (check)
- {
- if (ui.numGroupBox->isChecked())
- {
- ui.numGroupBox->setChecked(!check);
- }
- }
- }
- void ColumnEdit::slot_insertNumEnable(bool check)
- {
- if (check)
- {
- if (ui.textGroupBox->isChecked())
- {
- ui.textGroupBox->setChecked(!check);
- }
- }
- }
- void ColumnEdit::slot_addPrefix(int s)
- {
- if (s == Qt::Checked)
- {
- ui.prefix->setEnabled(true);
- }
- else
- {
- ui.prefix->setEnabled(false);
- }
- }
- void ColumnEdit::slot_bigChar(bool isCheck)
- {
- ui.capital->setEnabled(isCheck);
- }
- //自动调整当前窗口的状态
- QWidget* ColumnEdit::autoAdjustCurrentEditWin()
- {
- QWidget* pw = m_editTabWidget->currentWidget();
- if (m_curEditWin != pw)
- {
- m_curEditWin = pw;
- }
- return pw;
- }
- void ColumnEdit::setTabWidget(QTabWidget *editTabWidget)
- {
- m_editTabWidget = editTabWidget;
- }
- void ColumnEdit::slot_ok()
- {
- autoAdjustCurrentEditWin();
- ScintillaEditView* pEdit = dynamic_cast<ScintillaEditView*>(m_curEditWin);
- if (pEdit == nullptr || pEdit->isReadOnly())
- {
- QApplication::beep();
- return;
- }
- QString text;
- bool isNum = false;
- int numType = 10;
- int initNum = 0;
- int inc = ui.incNum->value();
- int repeNum = ui.repeNum->value();
- bool isAddPrefix = ui.addPrefix->isChecked();
- QString prefix = ui.prefix->text();
- bool isCapital = ui.capital->isChecked();
- //是插入文本模式
- if (ui.textGroupBox->isChecked())
- {
- text = ui.inputText->text();
- }
- else
- {
- isNum = true;
- text = ui.initNum->text();
- bool ok = false;
- if (ui.is10->isChecked())
- {
- numType = 10;
- }
- else if (ui.is16->isChecked())
- {
- numType = 16;
- }
- else if (ui.is8->isChecked())
- {
- numType = 8;
- }
- else if (ui.is2->isChecked())
- {
- numType = 2;
- }
- int num = text.toInt(&ok, 10);
- initNum = num;
- if (ok)
- {
- text = QString::number(num, numType);
- if (isAddPrefix)
- {
- text = prefix + text;
- }
- }
- else
- {
- //这里要加个提示
- QApplication::beep();
- return;
- }
- }
- pEdit->execute(SCI_BEGINUNDOACTION);
- if (ui.textGroupBox->isChecked())
- {
- if (pEdit->execute(SCI_SELECTIONISRECTANGLE) || pEdit->execute(SCI_GETSELECTIONS) > 1)
- {
- ColumnModeInfos colInfos = pEdit->getColumnModeSelectInfo();
- std::sort(colInfos.begin(), colInfos.end(), SortInPositionOrder());
- QByteArray bytes = text.toUtf8();
- pEdit->columnReplace(colInfos, bytes);
- std::sort(colInfos.begin(), colInfos.end(), SortInSelectOrder());
- pEdit->setMultiSelections(colInfos);
- return;
- }
- }
- else
- {
- if (pEdit->execute(SCI_SELECTIONISRECTANGLE) || pEdit->execute(SCI_GETSELECTIONS) > 1)
- {
- ColumnModeInfos colInfos = pEdit->getColumnModeSelectInfo();
- // If there is no column mode info available, no need to do anything
- // If required a message can be shown to user, that select column properly or something similar
- if (colInfos.size() > 0)
- {
- std::sort(colInfos.begin(), colInfos.end(), SortInPositionOrder());
- QByteArray bytes;
- if (isAddPrefix)
- {
- bytes = prefix.toUtf8();
- }
- pEdit->columnReplace(colInfos, initNum, inc, repeNum, numType, isCapital, bytes);
- std::sort(colInfos.begin(), colInfos.end(), SortInSelectOrder());
- pEdit->setMultiSelections(colInfos);
- }
- return;
- }
- }
- auto cursorPos = pEdit->execute(SCI_GETCURRENTPOS);
- auto cursorCol = pEdit->execute(SCI_GETCOLUMN, cursorPos);
- auto cursorLine = pEdit->execute(SCI_LINEFROMPOSITION, cursorPos);
- auto endPos = pEdit->execute(SCI_GETLENGTH);
- auto endLine = pEdit->execute(SCI_LINEFROMPOSITION, endPos);
-
- QByteArray lineData;
- int rn = repeNum;
- for (size_t i = cursorLine; i <= static_cast<size_t>(endLine); ++i)
- {
- auto lineBegin = pEdit->execute(SCI_POSITIONFROMLINE, i);
- auto lineEnd = pEdit->execute(SCI_GETLINEENDPOSITION, i);
- auto lineEndCol = pEdit->execute(SCI_GETCOLUMN, lineEnd);
- auto lineLen = lineEnd - lineBegin;
- lineData.resize(lineLen);
-
- Sci_TextRange lineText;
- lineText.chrg.cpMin = static_cast<Sci_Position>(lineBegin);
- lineText.chrg.cpMax = static_cast<Sci_Position>(lineEnd);
- lineText.lpstrText = lineData.data();
- //获取原始行的内容
- pEdit->SendScintilla(SCI_GETTEXTRANGE, 0, &lineText);
- if (lineEndCol < cursorCol)
- {
- QByteArray s_space(cursorCol - lineEndCol, ' ');
- lineData.append(s_space);
- lineData.append(text.toUtf8());
- }
- else
- {
- int posAbs2Start = pEdit->execute(SCI_FINDCOLUMN, i, cursorCol);
- int posRelative2Start = posAbs2Start - lineBegin;
- lineData.insert(posRelative2Start, text.toUtf8());
- }
- pEdit->SendScintilla(SCI_SETTARGETRANGE, lineBegin, lineEnd);
- pEdit->SendScintilla(SCI_REPLACETARGET, lineData.size(), lineData.data());
- if (isNum)
- {
- --rn;
- if (rn > 0)
- {
- }
- else
- {
- rn = repeNum;
- initNum += inc;
- }
-
- if (numType != 16)
- {
- text = QString::number(initNum, numType);
- }
- else
- {
- if (isCapital)
- {
- text = QString::number(initNum, numType).toUpper();
- }
- else
- {
- text = QString::number(initNum, numType);
- }
- }
- if (isAddPrefix)
- {
- text = prefix + text;
- }
- }
- }
- pEdit->execute(SCI_ENDUNDOACTION);
- }
|