1/*
2 * Copyright (C) 2020-2022 Roy Qu (royqh1979@gmail.com)
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17#include "caretlist.h"
18#include <QDebug>
19
20CaretList::CaretList(QObject* parent):
21 QObject(parent),
22 mIndex(-1),
23 mPauseAdd(false)
24{
25
26}
27
28void CaretList::addCaret(Editor *editor, int line, int aChar)
29{
30 if (mPauseAdd)
31 return;
32 for (int i=mList.count()-1;i>mIndex;i--) {
33 removeCaret(i);
34 }
35 PEditorCaret caret = std::make_shared<EditorCaret>();
36 caret->editor = editor;
37 caret->line = line;
38 caret->aChar = aChar;
39 mList.append(caret);
40 mIndex++;
41 //qDebug()<<"add caret:"<<mIndex<<":"<<mList.count();
42}
43
44bool CaretList::hasPrevious() const
45{
46 //qDebug()<<"has previous:"<<mIndex<<":"<<mList.count();
47 return mIndex>0;
48}
49
50bool CaretList::hasNext() const
51{
52 //qDebug()<<"has next:"<<mIndex<<":"<<mList.count();
53 return mIndex<mList.count()-1;
54}
55
56PEditorCaret CaretList::gotoAndGetPrevious()
57{
58 if (!hasPrevious())
59 return PEditorCaret();
60 mIndex--;
61 //qDebug()<<"move previous:"<<mIndex<<":"<<mList.count();
62 if (mIndex<mList.count())
63 return mList[mIndex];
64 return PEditorCaret();
65}
66
67PEditorCaret CaretList::gotoAndGetNext()
68{
69 if (!hasNext())
70 return PEditorCaret();
71 mIndex++;
72 //qDebug()<<"move next:"<<mIndex<<":"<<mList.count();
73 if (mIndex>=0)
74 return mList[mIndex];
75 return PEditorCaret();
76}
77
78void CaretList::removeEditor(const Editor *editor)
79{
80 for (int i = mList.count()-1;i>=0;i--) {
81 if (mList[i]->editor == editor)
82 removeCaret(i);
83 }
84}
85
86void CaretList::reset()
87{
88 mList.clear();
89 mIndex = -1;
90}
91
92void CaretList::pause()
93{
94 mPauseAdd = true;
95}
96
97void CaretList::unPause()
98{
99 mPauseAdd = false;
100}
101
102void CaretList::linesDeleted(const Editor *editor, int firstLine, int count)
103{
104 //qDebug()<<"deleted:"<<mIndex<<":"<<mList.count();
105 for (int i=mList.count()-1;i>=0;i--) {
106 if (mList[i]->editor == editor
107 && mList[i]->line>=firstLine) {
108 if (mList[i]->line < (firstLine+count))
109 removeCaret(i);
110 else
111 mList[i]->line-=count;
112 }
113 }
114}
115
116void CaretList::linesInserted(const Editor *editor, int firstLine, int count)
117{
118 //qDebug()<<"inserted:"<<mIndex<<":"<<mList.count();
119 for(PEditorCaret& caret:mList) {
120 if (caret->editor == editor
121 && caret->line >= firstLine)
122 caret->line+=count;
123 }
124}
125
126void CaretList::removeCaret(int index)
127{
128 if (index<0 || index>=mList.count())
129 return;
130 mList.removeAt(index);
131 if (mIndex>=index)
132 mIndex--;
133}
134