File: tabledialog.py

package info (click to toggle)
retext 8.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,780 kB
  • sloc: python: 5,363; xml: 149; makefile: 20; sh: 8
file content (77 lines) | stat: -rw-r--r-- 3,257 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# This file is part of ReText
# Copyright: 2018 Changhee Kim, 2018-2024 Dmitry Shachnev
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from markups import ReStructuredTextMarkup
from PyQt6.QtCore import Qt
from PyQt6.QtWidgets import QDialog, QDialogButtonBox, QGridLayout, QLabel, QSpinBox


class InsertTableDialog(QDialog):
    def __init__(self, parent):
        QDialog.__init__(self, parent)
        self.parent = parent
        self.setWindowTitle(self.tr('Insert table'))
        buttonBox = QDialogButtonBox(self)
        buttonBox.setStandardButtons(QDialogButtonBox.StandardButton.Ok |
                                     QDialogButtonBox.StandardButton.Cancel)
        buttonBox.accepted.connect(self.makeTable)
        buttonBox.rejected.connect(self.close)

        layout = QGridLayout(self)

        rowsLabel = QLabel(self.tr('Number of rows') + ':', self)
        columnsLabel = QLabel(self.tr('Number of columns') + ':', self)
        self.rowsSpinBox = QSpinBox(self)
        self.columnsSpinBox = QSpinBox(self)

        self.rowsSpinBox.setRange(1, 10)
        self.columnsSpinBox.setRange(1, 10)
        self.rowsSpinBox.setValue(3)
        self.columnsSpinBox.setValue(3)

        layout.addWidget(rowsLabel, 0, 0)
        layout.addWidget(self.rowsSpinBox, 0, 1, Qt.AlignmentFlag.AlignRight)
        layout.addWidget(columnsLabel, 1, 0)
        layout.addWidget(self.columnsSpinBox, 1, 1, Qt.AlignmentFlag.AlignRight)
        layout.addWidget(buttonBox, 2, 0, 1, 2)

    def makeTable(self):
        rowsCount = self.rowsSpinBox.value()
        columnsCount = self.columnsSpinBox.value() + 1

        tab = self.parent.currentTab
        cursor = tab.editBox.textCursor()

        tableCode = '' if cursor.atBlockStart() else '\n\n'
        if tab.activeMarkupClass == ReStructuredTextMarkup:
            # Insert reStructuredText grid table
            tableCode += '-----'.join('+' * columnsCount) + '\n'
            tableCode += '     '.join('|' * columnsCount) + '\n'
            tableCode += '====='.join('+' * columnsCount) + '\n'
            tableCode += ('     '.join('|' * columnsCount) + '\n' +
                          '-----'.join('+' * columnsCount) + '\n') * rowsCount
        else:
            # Insert Markdown table
            tableCode += '     '.join('|' * columnsCount) + '\n'
            tableCode += '-----'.join('|' * columnsCount) + '\n'
            tableCode += ('     '.join('|' * columnsCount) + '\n') * rowsCount

        cursor.insertText(tableCode)
        self.close()

        # Activate the Table editing mode
        self.parent.actionTableMode.setChecked(True)
        tab.editBox.tableModeEnabled = True