1# This file is part of ReText
2# Copyright: 2018 Changhee Kim, 2018-2021 Dmitry Shachnev
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 2 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 <http://www.gnu.org/licenses/>.
16
17from markups import ReStructuredTextMarkup
18from PyQt5.QtCore import Qt
19from PyQt5.QtWidgets import QDialog, QDialogButtonBox, QGridLayout, QLabel, \
20    QSpinBox
21
22
23class InsertTableDialog(QDialog):
24    def __init__(self, parent):
25        QDialog.__init__(self, parent)
26        self.parent = parent
27        self.setWindowTitle(self.tr('Insert table'))
28        buttonBox = QDialogButtonBox(self)
29        buttonBox.setStandardButtons(QDialogButtonBox.StandardButton.Ok |
30                                     QDialogButtonBox.StandardButton.Cancel)
31        buttonBox.accepted.connect(self.makeTable)
32        buttonBox.rejected.connect(self.close)
33
34        layout = QGridLayout(self)
35
36        rowsLabel = QLabel(self.tr('Number of rows') + ':', self)
37        columnsLabel = QLabel(self.tr('Number of columns') + ':', self)
38        self.rowsSpinBox = QSpinBox(self)
39        self.columnsSpinBox = QSpinBox(self)
40
41        self.rowsSpinBox.setRange(1, 10)
42        self.columnsSpinBox.setRange(1, 10)
43        self.rowsSpinBox.setValue(3)
44        self.columnsSpinBox.setValue(3)
45
46        layout.addWidget(rowsLabel, 0, 0)
47        layout.addWidget(self.rowsSpinBox, 0, 1, Qt.AlignmentFlag.AlignRight)
48        layout.addWidget(columnsLabel, 1, 0)
49        layout.addWidget(self.columnsSpinBox, 1, 1, Qt.AlignmentFlag.AlignRight)
50        layout.addWidget(buttonBox, 2, 0, 1, 2)
51
52    def makeTable(self):
53        rowsCount = self.rowsSpinBox.value()
54        columnsCount = self.columnsSpinBox.value() + 1
55
56        tab = self.parent.currentTab
57        cursor = tab.editBox.textCursor()
58
59        tableCode = '' if cursor.atBlockStart() else '\n\n'
60        if tab.activeMarkupClass == ReStructuredTextMarkup:
61            # Insert reStructuredText grid table
62            tableCode += '-----'.join('+' * columnsCount) + '\n'
63            tableCode += '     '.join('|' * columnsCount) + '\n'
64            tableCode += '====='.join('+' * columnsCount) + '\n'
65            tableCode += ('     '.join('|' * columnsCount) + '\n' +
66                          '-----'.join('+' * columnsCount) + '\n') * rowsCount
67        else:
68            # Insert Markdown table
69            tableCode += '     '.join('|' * columnsCount) + '\n'
70            tableCode += '-----'.join('|' * columnsCount) + '\n'
71            tableCode += ('     '.join('|' * columnsCount) + '\n') * rowsCount
72
73        cursor.insertText(tableCode)
74        self.close()
75
76        # Activate the Table editing mode
77        self.parent.actionTableMode.setChecked(True)
78        tab.editBox.tableModeEnabled = True
79