~ubuntu-branches/ubuntu/vivid/frescobaldi/vivid

« back to all changes in this revision

Viewing changes to frescobaldi_app/sessions/dialog.py

  • Committer: Package Import Robot
  • Author(s): Ryan Kavanagh
  • Date: 2012-01-03 16:20:11 UTC
  • mfrom: (1.4.1)
  • Revision ID: package-import@ubuntu.com-20120103162011-tsjkwl4sntwmprea
Tags: 2.0.0-1
* New upstream release 
* Drop the following uneeded patches:
  + 01_checkmodules_no_python-kde4_build-dep.diff
  + 02_no_pyc.diff
  + 04_no_binary_lilypond_upgrades.diff
* Needs new dependency python-poppler-qt4
* Update debian/watch for new download path
* Update copyright file with new holders and years

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# This file is part of the Frescobaldi project, http://www.frescobaldi.org/
 
2
#
 
3
# Copyright (c) 2008 - 2011 by Wilbert Berendsen
 
4
#
 
5
# This program is free software; you can redistribute it and/or
 
6
# modify it under the terms of the GNU General Public License
 
7
# as published by the Free Software Foundation; either version 2
 
8
# of the License, or (at your option) any later version.
 
9
#
 
10
# This program is distributed in the hope that it will be useful,
 
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
# GNU General Public License for more details.
 
14
#
 
15
# You should have received a copy of the GNU General Public License
 
16
# along with this program; if not, write to the Free Software
 
17
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
18
# See http://www.gnu.org/licenses/ for more information.
 
19
 
 
20
"""
 
21
Session dialog for named session stuff.
 
22
"""
 
23
 
 
24
from __future__ import unicode_literals
 
25
 
 
26
 
 
27
from PyQt4.QtGui import (
 
28
    QCheckBox, QDialog, QDialogButtonBox, QGridLayout, QLabel, QLineEdit,
 
29
    QMessageBox, QVBoxLayout)
 
30
 
 
31
import app
 
32
import widgets.listedit
 
33
import widgets.urlrequester
 
34
import sessions
 
35
 
 
36
 
 
37
class SessionManagerDialog(QDialog):
 
38
    def __init__(self, mainwindow):
 
39
        super(SessionManagerDialog, self).__init__(mainwindow)
 
40
        self.setWindowTitle(app.caption(_("Manage Sessions")))
 
41
        layout = QVBoxLayout()
 
42
        self.setLayout(layout)
 
43
        
 
44
        self.sessions = SessionList(self)
 
45
        layout.addWidget(self.sessions)
 
46
        layout.addWidget(widgets.Separator())
 
47
        
 
48
        self.buttons = b = QDialogButtonBox(self)
 
49
        layout.addWidget(b)
 
50
        b.setStandardButtons(QDialogButtonBox.Help | QDialogButtonBox.Close)
 
51
        b.rejected.connect(self.accept)
 
52
        self.sessions.load()
 
53
 
 
54
 
 
55
class SessionList(widgets.listedit.ListEdit):
 
56
    """Manage the list of sessions."""
 
57
    def load(self):
 
58
        names = sessions.sessionNames()
 
59
        current = sessions.currentSession()
 
60
        self.setValue(names)
 
61
        if current in names:
 
62
            self.setCurrentRow(names.index(current))
 
63
 
 
64
    def removeItem(self, item):
 
65
        sessions.deleteSession(item.text())
 
66
        super(SessionList, self).removeItem(item)
 
67
 
 
68
    def openEditor(self, item):
 
69
        name = SessionEditor(self).edit(item.text())
 
70
        if name:
 
71
            item.setText(name)
 
72
            return True
 
73
 
 
74
 
 
75
class SessionEditor(QDialog):
 
76
    def __init__(self, parent=None):
 
77
        super(SessionEditor, self).__init__(parent)
 
78
        
 
79
        layout = QVBoxLayout()
 
80
        self.setLayout(layout)
 
81
        
 
82
        grid = QGridLayout()
 
83
        layout.addLayout(grid)
 
84
        
 
85
        self.name = QLineEdit()
 
86
        self.nameLabel = l = QLabel()
 
87
        l.setBuddy(self.name)
 
88
        grid.addWidget(l, 0, 0)
 
89
        grid.addWidget(self.name, 0, 1)
 
90
        
 
91
        self.autosave = QCheckBox()
 
92
        grid.addWidget(self.autosave, 1, 1)
 
93
        
 
94
        self.basedir = widgets.urlrequester.UrlRequester()
 
95
        self.basedirLabel = l = QLabel()
 
96
        l.setBuddy(self.basedir)
 
97
        grid.addWidget(l, 2, 0)
 
98
        grid.addWidget(self.basedir, 2, 1)
 
99
        
 
100
        layout.addWidget(widgets.Separator())
 
101
        self.buttons = b = QDialogButtonBox(self)
 
102
        layout.addWidget(b)
 
103
        b.setStandardButtons(QDialogButtonBox.Help | QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
 
104
        b.accepted.connect(self.accept)
 
105
        b.rejected.connect(self.reject)
 
106
        app.translateUI(self)
 
107
        
 
108
    def translateUI(self):
 
109
        self.nameLabel.setText(_("Name:"))
 
110
        self.autosave.setText(_("Always save the list of documents in this session"))
 
111
        self.basedirLabel.setText(_("Base directory:"))
 
112
    
 
113
    def load(self, name):
 
114
        settings = sessions.sessionGroup(name)
 
115
        self.autosave.setChecked(settings.value("autosave", True) not in (False, 'false'))
 
116
        self.basedir.setPath(settings.value("basedir", ""))
 
117
        # more settings here
 
118
        
 
119
    def save(self, name):
 
120
        settings = sessions.sessionGroup(name)
 
121
        settings.setValue("autosave", self.autosave.isChecked())
 
122
        settings.setValue("basedir", self.basedir.path())
 
123
        # more settings here
 
124
        
 
125
    def defaults(self):
 
126
        self.autosave.setChecked(True)
 
127
        self.basedir.setPath('')
 
128
        # more defaults here
 
129
        
 
130
    def edit(self, name=None):
 
131
        self._originalName = name
 
132
        if name:
 
133
            caption = _("Edit session: {name}").format(name=name)
 
134
            self.name.setText(name)
 
135
            self.load(name)
 
136
        else:
 
137
            caption = _("Edit new session")
 
138
            self.name.clear()
 
139
            self.name.setFocus()
 
140
            self.defaults()
 
141
        self.setWindowTitle(app.caption(caption))
 
142
        if self.exec_():
 
143
            # name changed?
 
144
            name = self.name.text()
 
145
            if self._originalName and name != self._originalName:
 
146
                sessions.renameSession(self._originalName, name)
 
147
            self.save(name)
 
148
            return name
 
149
 
 
150
    def done(self, result):
 
151
        if not result or self.validate():
 
152
            super(SessionEditor, self).done(result)
 
153
        
 
154
    def validate(self):
 
155
        """Checks if the input is acceptable.
 
156
        
 
157
        If this method returns True, the dialog is accepted when OK is clicked.
 
158
        Otherwise a messagebox could be displayed, and the dialog will remain
 
159
        visible.
 
160
        """
 
161
        name = self.name.text().strip()
 
162
        self.name.setText(name)
 
163
        if not name:
 
164
            self.name.setFocus()
 
165
            QMessageBox.warning(self, app.caption(_("Warning")),
 
166
                _("Please enter a session name."))
 
167
            if self._originalName:
 
168
                self.name.setText(self._originalName)
 
169
            return False
 
170
        
 
171
        elif name == 'none':
 
172
            self.name.setFocus()
 
173
            QMessageBox.warning(self, app.caption(_("Warning")),
 
174
                _("Please do not use the name '{name}'.".format(name="none")))
 
175
            return False
 
176
        
 
177
        elif self._originalName != name and name in sessions.sessionNames():
 
178
            self.name.setFocus()
 
179
            box = QMessageBox(QMessageBox.Warning, app.caption(_("Warning")),
 
180
                _("Another session with the name {name} already exists.\n\n"
 
181
                  "Do you want to overwrite it?").format(name=name),
 
182
                QMessageBox.Discard | QMessageBox.Cancel, self)
 
183
            box.button(QMessageBox.Discard).setText(_("Overwrite"))
 
184
            result = box.exec_()
 
185
            if result != QMessageBox.Discard:
 
186
                return False
 
187
            
 
188
        return True
 
189