class AboutDialog(QDialog): """ About Dialog for Embroidermodder. """ def __init__(self, parent=None): """Default class constructor.""" super(AboutDialog, self).__init__(parent) p = self.palette() p.setColor(self.backgroundRole(), Qt.white) self.setPalette(p) if parent: self.gImgDir = parent.gImgDir self.gIconDir = parent.gIconDir elif __name__ == '__main__': self.gImgDir = gAppDir + os.sep + 'images' self.gIconDir = gAppDir + os.sep + 'icons' + os.sep + 'default' # The tiled theme background texture. self.bgLogo = QPixmap(self.gImgDir + os.sep + 'texture-spirals.png') self.bgBrush = QBrush(self.bgLogo) self.setWhatsThis(self.tr("""\ The background is a tiled image of an actual design that was stitched out during the pre-alpha stage. It was created by Nina Paley and Theodore Gray using Mathematica in conjunction with our software. They have graciously allowed us to use it for the project in whichever way we wish. We thought it looked so good, that it has become the new theme for Embroidermodder 2. To check out some of the more interesting embroidery projects they are working on, visit http://blog.ninapaley.com/""")) self.imgLbl = EmbroidermodderLogo(self) aboutLbl = QTextBrowser(self) aboutLbl.setReadOnly(True) aboutLbl.setOpenExternalLinks(True) aboutLbl.setText('<b>%s</b>' % '<br>'.join(__doc__.split('\n'))) aboutLbl.setWhatsThis(self.tr('This is the AWESOME people that brought Embroidermodder 2 to life.')) # We want very slight opacity of the white background # so the seamless texture shows slightly through. opacityStyleSheet = """\ QTextEdit:read-only { color: rgb(50, 50, 50); font-size: 12px; font-weight: bold; background-color: rgba(255, 255, 255, 240); border: 1px solid rgba(0, 0, 0, 255); } """ aboutLbl.setStyleSheet(opacityStyleSheet) op = QGraphicsOpacityEffect(aboutLbl) op.setOpacity(0.95) aboutLbl.setGraphicsEffect(op) self.notebook = QTabWidget(self) self.notebook.setMinimumWidth(500) self.notebook.addTab(aboutLbl, self.tr('About')) self.notebook.setTabIcon(0, QIcon(self.gIconDir + os.sep + 'app.png')) self.notebook.setTabIcon(1, QIcon(self.gImgDir + os.sep + 'kickstarter-logo-k-color.png')) notebookStyleSheet = """\ QTabWidget::pane { /* The tab widget frame */ border-top: 1px solid #000000; position: absolute; top: -0.5em; } QTabWidget::tab-bar { alignment: center; } /* Style the tab using the tab sub-control. Note that it reads QTabBar _not_ QTabWidget */ QTabBar::tab { margin-top: 2px; /* make non-selected tabs look smaller */ font-size: 14px; font-weight: bold; background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #E1E1E1, stop: 0.4 #DDDDDD, stop: 0.5 #D8D8D8, stop: 1.0 #D3D3D3); border: 1px solid #000000; /* border-bottom-color: #C2C7CB; */ /* same as the pane color */ border-top-left-radius: 4px; border-top-right-radius: 4px; min-width: 40ex; min-height: 5ex; padding: 3px; } QTabBar::tab:selected { margin-top: 0px; font-size: 16px; font-weight: bold; background: qlineargradient(x1: 0, y1: 0, x2: 2, y2: 2, stop: 0 #0C6AB0, stop: 0.15 #55C4E6, stop: 0.15 #55C4E6, stop: 0.5 #FFFFFF, stop: 0.5 #FFFFFF, stop: 0.85 #55C4E6, stop: 0.85 #55C4E6, stop: 1.0 #0C6AB0); border: 1px solid #000000; } QTabBar::tab:!selected:hover { margin-top: 2px; /* make non-selected tabs look smaller */ font-size: 14px; font-weight: bold; background: qlineargradient(x1: 0, y1: 0, x2: 2, y2: 2, stop: 0 #888888, stop: 0.15 #BBBBBB, stop: 0.15 #BBBBBB, stop: 0.5 #FFFFFF, stop: 0.5 #FFFFFF, stop: 0.85 #BBBBBB, stop: 0.85 #BBBBBB, stop: 1.0 #888888); border: 1px solid #000000; } QTabBar::tab:selected { border-color: #000000; border-bottom-color: #000000; /* same as pane color */ } """ self.notebook.setStyleSheet(notebookStyleSheet) self.notebook.currentChanged.connect(self.CurrentTabChanged) buttonbox = QDialogButtonBox(Qt.Horizontal, self) button = QPushButton(self) button.setText(self.tr("Oh, Yeah!")) button.setWhatsThis(self.tr('This is the Oh, Yeah! button!') + '\n' + self.tr('Oh, Yeah!')) buttonbox.addButton(button, QDialogButtonBox.AcceptRole) buttonbox.setCenterButtons(True) buttonbox.accepted.connect(self.accept) hbLayout1 = QHBoxLayout() hbLayout2 = QHBoxLayout() vbLayout = QVBoxLayout() hbLayout1.addStretch() hbLayout1.addWidget(self.imgLbl) hbLayout1.addStretch() hbLayout2.addStretch() hbLayout2.addWidget(self.notebook) hbLayout2.addStretch() vbLayout.addLayout(hbLayout1) vbLayout.addLayout(hbLayout2) vbLayout.addWidget(buttonbox) self.setLayout(vbLayout) self.setWindowTitle(self.tr('About Embroidermodder Version 2.0')) QApplication.restoreOverrideCursor() # TODO/???/PORT# don't mess with the window resize cursors. # def __del__(self): # """Class destructor""" # QApplication.restoreOverrideCursor() def CurrentTabChanged(self): tt = self.notebook.tabText(self.notebook.currentIndex()) if tt == self.tr('About'): self.imgLbl.paintThisText = 'About' elif tt == self.tr('Supporters'): self.imgLbl.paintThisText = 'Kickstarter' self.imgLbl.repaint() def paintEvent(self, event): """ Handles the ``paintEvent`` event for :class:`AboutDialog`. :param `event`: a `QPaintEvent` event to be processed. """ rect = self.rect() painter = QPainter(self) painter.setRenderHint(painter.SmoothPixmapTransform) # Always fill with a solid color first painter.fillRect(rect, Qt.white) # Then overlay the texture painter.fillRect(rect, self.bgBrush)
class window(QMainWindow): """Main window.""" def __init__(self, parent=None): """Initialize the parent class of this instance.""" super(window, self).__init__(parent) app.aboutToQuit.connect(self.myExitHandler) self.style_sheet = self.styleSheet('style') # app.setStyle(QStyleFactory.create('Macintosh')) #app.setStyleSheet(self.style_sheet) self.layout().setSpacing(0) self.layout().setContentsMargins(0,0,0,0) app.setOrganizationName("Eivind Arvesen") app.setOrganizationDomain("https://github.com/eivind88/raskolnikov") app.setApplicationName("Raskolnikov") app.setApplicationVersion("0.0.1") settings = QSettings() self.data_location = QDesktopServices.DataLocation self.temp_location = QDesktopServices.TempLocation self.cache_location = QDesktopServices.CacheLocation self.startpage = "https://duckduckgo.com/" self.new_tab_behavior = "insert" global bookmarks global saved_tabs print "Currently saved_tabs:\n", saved_tabs global menubar menubar = QMenuBar() # Initialize a statusbar for the window self.statusbar = self.statusBar() self.statusbar.setFont(QFont("Helvetica Neue", 11, QFont.Normal)) self.statusbar.setStyleSheet(self.style_sheet) self.statusbar.setMinimumHeight(15) self.pbar = QProgressBar() self.pbar.setMaximumWidth(100) self.statusbar.addPermanentWidget(self.pbar) self.statusbar.hide() self.setMinimumSize(504, 235) # self.setWindowModified(True) # app.alert(self, 0) self.setWindowTitle("Raskolnikov") # toolbar = self.addToolBar('Toolbar') # toolbar.addAction(exitAction) # self.setUnifiedTitleAndToolBarOnMac(True) self.setWindowIcon(QIcon("")) # Create input widgets self.bbutton = QPushButton(u"<") self.fbutton = QPushButton(u">") self.hbutton = QPushButton(u"⌂") self.edit = QLineEdit("") self.edit.setFont(QFont("Helvetica Neue", 12, QFont.Normal)) self.edit.setPlaceholderText("Enter URL") # self.edit.setMinimumSize(400, 24) self.rbutton = QPushButton(u"↻") self.dbutton = QPushButton(u"☆") self.tbutton = QPushButton(u"⁐") # ↆ ⇧ √ ⌘ ⏎ ⏏ ⚠ ✓ ✕ ✖ ✗ ✘ ::: ❤ ☮ ☢ ☠ ✔ ☑ ♥ ✉ ☣ ☤ ✘ ☒ ♡ ツ ☼ ☁ ❅ ✎ self.nbutton = QPushButton(u"+") self.nbutton.setObjectName("NewTab") self.nbutton.setMinimumSize(35, 30) self.nbutton.setSizePolicy(QSizePolicy.Minimum,QSizePolicy.Minimum) self.edit.setTextMargins(2, 1, 2, 0) # create a horizontal layout for the input input_layout = QHBoxLayout() input_layout.setSpacing(4) input_layout.setContentsMargins(0, 0, 0, 0) # add the input widgets to the input layout input_layout.addWidget(self.bbutton) input_layout.addWidget(self.fbutton) input_layout.addWidget(self.hbutton) input_layout.addWidget(self.edit) input_layout.addWidget(self.rbutton) input_layout.addWidget(self.dbutton) input_layout.addWidget(self.tbutton) # create a widget to hold the input layout self.input_widget = QFrame() self.input_widget.setObjectName("InputWidget") self.input_widget.setStyleSheet(self.style_sheet) # set the layout of the widget self.input_widget.setLayout(input_layout) self.input_widget.setVisible(True) # CREATE BOOKMARK-LINE HERE self.bookmarks_layout = QHBoxLayout() self.bookmarks_layout.setSpacing(0) self.bookmarks_layout.setContentsMargins(0, 0, 0, 0) for i in bookmarks: link = QToolButton() link.setDefaultAction(QAction(unicode(i), link)) link.setObjectName(unicode(i)) link.setFont(QFont("Helvetica Neue", 11, QFont.Normal)) link.clicked.connect(self.handleBookmarks) self.bookmarks_layout.addWidget(link) self.bookmarks_widget = QFrame() self.bookmarks_widget.setObjectName("BookmarkWidget") self.bookmarks_widget.setStyleSheet(self.style_sheet) self.bookmarks_widget.setLayout(self.bookmarks_layout) if not bookmarks: self.bookmarks_widget.hide() # Task list self.tasklist = QStandardItemModel() #parentItem = self.tasklist.invisibleRootItem() #self.tasklist.header().hide() self.tasklist.setHorizontalHeaderItem(0, QStandardItem('Tasks')) parentItem = QStandardItem("Parent") self.tasklist.appendRow(parentItem) for i in range(4): item = QStandardItem("Item %d" % i) parentItem.appendRow(item) #parentItem = item #self.list.activated[str].connect(self.handleBookmarks) #self.list.view().setSizePolicy(QSizePolicy.Minimum,QSizePolicy.Minimum) # create tabs self.tabs = QTabWidget() self.tabs.setDocumentMode(True) self.tabs.setTabsClosable(True) self.tabs.setMovable(True) self.tabs.tabBar().hide() self.tabs.setFont(QFont("Helvetica Neue", 11, QFont.Normal)) self.tabs.setCornerWidget(self.nbutton) self.tabs.cornerWidget().setObjectName("CornerWidget") self.tabs.cornerWidget().setMinimumSize(10, 24) self.tabs.cornerWidget().setSizePolicy(QSizePolicy.Minimum,QSizePolicy.Minimum) if saved_tabs: for tab in saved_tabs['tabs']: tasklist = QTreeView() tasklist.hide() tasklist.setObjectName('taskList') tasklist.setMinimumWidth(100) tasklist.setMaximumWidth(250) new_tab = QWebView() new_tab.setObjectName('webView') inspector = QWebInspector(self) inspector.setObjectName('webInspector') inspector.hide() page_layout = QVBoxLayout() page_layout.setSpacing(0) page_layout.setContentsMargins(0, 0, 0, 0) page_layout.addWidget(new_tab) page_layout.addWidget(inspector) page_widget = QFrame() page_widget.setObjectName('pageWidget') page_widget.setLayout(page_layout) complete_tab_layout = QHBoxLayout() complete_tab_layout.setSpacing(0) complete_tab_layout.setContentsMargins(0, 0, 0, 0) complete_tab_layout.addWidget(tasklist) complete_tab_layout.addWidget(page_widget) complete_tab_widget = QFrame() complete_tab_widget.setLayout(complete_tab_layout) #for page in tab['history']: # new_tab.load(QUrl(page['url'])) #print tab['current_history'] #for item in new_tab.history().items(): # print item #new_tab.history().goToItem(new_tab.history().itemAt(tab['current_history'])) new_tab.load(QUrl(tab['history'][tab['current_history']]['url'])) tab['current_history'] self.tabs.setUpdatesEnabled(False) if self.new_tab_behavior == "insert": self.tabs.insertTab(self.tabs.currentIndex()+1, complete_tab_widget, unicode(new_tab.title())) elif self.new_tab_behavior == "append": self.tabs.appendTab(complete_tab_widget, unicode(new_tab.title())) self.tabs.setUpdatesEnabled(True) new_tab.titleChanged.connect(self.change_tab) new_tab.urlChanged.connect(self.change_tab) new_tab.loadStarted.connect(self.load_start) new_tab.loadFinished.connect(self.load_finish) new_tab.loadProgress.connect(self.pbar.setValue) new_tab.page().linkHovered.connect(self.linkHover) inspector.setPage(new_tab.page()) for index, tab in enumerate(saved_tabs['tabs']): self.tabs.setTabText(index, tab['history'][tab['current_history']]['title']) self.tabs.setCurrentIndex(saved_tabs['current_tab']) else: self.new_tab() tabs_layout = QVBoxLayout() tabs_layout.setSpacing(0) tabs_layout.setContentsMargins(0, 0, 0, 0) tabs_layout.addWidget(self.tabs) self.tabs_widget = QFrame() self.tabs_widget.setObjectName("TabLine") self.tabs_widget.setStyleSheet(self.style_sheet) self.tabs_widget.setLayout(tabs_layout) self.tabs_widget.setVisible(True) # Webkit settings gsettings = self.tabs.currentWidget().findChild(QFrame, unicode('pageWidget')).findChild(QWebView, unicode('webView')).settings().globalSettings() # Basic settings gsettings.setAttribute(QWebSettings.AutoLoadImages, True) gsettings.setAttribute(QWebSettings.JavascriptEnabled, True) gsettings.setAttribute(QWebSettings.JavascriptCanOpenWindows, False) gsettings.setAttribute(QWebSettings.JavascriptCanAccessClipboard, False) gsettings.setAttribute(QWebSettings.PluginsEnabled, False) # Flash isn't stable at present gsettings.setAttribute(QWebSettings.JavaEnabled, False) # Java applet's aren't supported by PySide gsettings.setAttribute(QWebSettings.DeveloperExtrasEnabled, True) gsettings.setAttribute(QWebSettings.AcceleratedCompositingEnabled, True) # Performace settings gsettings.setAttribute(QWebSettings.DnsPrefetchEnabled, True) gsettings.setAttribute(QWebSettings.AcceleratedCompositingEnabled, True) gsettings.setAttribute(QWebSettings.DnsPrefetchEnabled, True) # Other settings gsettings.setAttribute(QWebSettings.PrivateBrowsingEnabled, False) # Create a vertical layout and add widgets vlayout = QVBoxLayout() vlayout.setSpacing(0) vlayout.setContentsMargins(0, 0, 0, 0) # toolbar.addWidget(self.input_widget) vlayout.addWidget(self.input_widget) vlayout.addWidget(self.bookmarks_widget) vlayout.addWidget(self.tabs_widget) # create a widget to hold the vertical layout wrapper_widget = QWidget() wrapper_widget.setLayout(vlayout) self.setCentralWidget(wrapper_widget) self.bbutton.clicked.connect(self.tabs.currentWidget().findChild(QFrame, unicode('pageWidget')).findChild(QWebView, unicode('webView')).back) self.fbutton.clicked.connect(self.tabs.currentWidget().findChild(QFrame, unicode('pageWidget')).findChild(QWebView, unicode('webView')).forward) self.hbutton.clicked.connect(self.goHome) self.edit.returnPressed.connect(self.set_url) # Add button signal to "go" slot self.rbutton.clicked.connect(self.tabs.currentWidget().findChild(QFrame, unicode('pageWidget')).findChild(QWebView, unicode('webView')).reload) self.dbutton.clicked.connect(self.bookmark) self.tbutton.clicked.connect(self.toggleTaskBar) self.nbutton.clicked.connect(self.new_tab) self.tabs.tabCloseRequested.connect(self.tabs.removeTab) self.tabs.currentChanged.connect(self.change_tab) widgets = (input_layout.itemAt(i).widget() for i in range( input_layout.count())) for widget in widgets: if isinstance(widget, QPushButton): widget.setFixedSize(33, 21) widget.setFont(QFont("Helvetica Neue", 12, QFont.Normal)) widget.pressed.connect(self.press_button) widget.released.connect(self.release_button) # make a ctrl+q quit sequence = QKeySequence(Qt.CTRL + Qt.Key_Q) QShortcut(sequence, self, SLOT("close()")) # make an accelerator to toggle fullscreen sequence = QKeySequence(Qt.CTRL + Qt.SHIFT + Qt.Key_F) QShortcut(sequence, self, self.toggle_fullscreen) # make an accelerator to toggle input visibility sequence = QKeySequence(Qt.CTRL + Qt.ALT + Qt.Key_L) QShortcut(sequence, self, self.toggle_input) # make an accelerator to focus adress-bar sequence = QKeySequence(Qt.CTRL + Qt.Key_L) QShortcut(sequence, self, self.focus_adress) # make an accelerator to reload page sequence = QKeySequence(Qt.CTRL + Qt.Key_R) QShortcut(sequence, self, self.tabs.currentWidget().findChild(QFrame, unicode('pageWidget')).findChild(QWebView, unicode('webView')).reload) # make an accelerator to create new tab sequence = QKeySequence(Qt.CTRL + Qt.Key_T) QShortcut(sequence, self, self.new_tab) # make an accelerator to close tab sequence = QKeySequence(Qt.CTRL + Qt.Key_W) QShortcut(sequence, self, self.close_tab) # make an accelerator to navigate tabs sequence = QKeySequence(Qt.CTRL + Qt.SHIFT + Qt.Key_Left) QShortcut(sequence, self, self.previous_tab) sequence = QKeySequence(Qt.CTRL + Qt.SHIFT + Qt.Key_Right) QShortcut(sequence, self, self.next_tab) # make an accelerator to toggle inspector sequence = QKeySequence(Qt.CTRL + Qt.ALT + Qt.Key_U) QShortcut(sequence, self, self.handleShowInspector) # make an accelerator to toggle bookmark sequence = QKeySequence(Qt.CTRL + Qt.Key_D) QShortcut(sequence, self, self.bookmark) # make an accelerator to toggle task/project-list sequence = QKeySequence(Qt.CTRL + Qt.SHIFT + Qt.Key_L) QShortcut(sequence, self, self.toggleTaskBar) # finally set the attribute need to rotate # try: # self.setAttribute(Qt.WA_Maemo5AutoOrientation, True) # except: # print "not maemo" self.statusbar.show() def press_button(self): """On button press. Connected.""" self.sender().setStyleSheet('background-color: rgba(228, 228, 228)') def release_button(self): """On button release. Connected.""" self.sender().setStyleSheet('background-color: rgba(252, 252, 252)') def goHome(self): """Go to startpage.""" self.tabs.currentWidget().findChild(QFrame, unicode('pageWidget')).findChild(QWebView, unicode('webView')).setUrl(QUrl(self.startpage)) def handleShowInspector(self): """Toggle web inspector.""" self.tabs.currentWidget().findChild(QFrame, unicode('pageWidget')).findChild(QWebInspector, unicode('webInspector')).setShown(self.tabs.currentWidget().findChild(QFrame, unicode('pageWidget')).findChild(QWebInspector, unicode('webInspector')).isHidden()) def toggleTaskBar(self): """Toggle task bar.""" if self.tabs.currentWidget().findChild(QTreeView, unicode('taskList')).isHidden(): self.tabs.currentWidget().findChild(QTreeView, unicode('taskList')).setModel(self.tasklist) self.tabs.currentWidget().findChild(QTreeView, unicode('taskList')).setShown(self.tabs.currentWidget().findChild(QTreeView, unicode('taskList')).isHidden()) #self.tasklist.setShown(self.tasklist.isHidden()) def focus_adress(self): """Focus adress bar.""" self.edit.selectAll() self.edit.setFocus() def toggle_input(self): """Toggle input visibility.""" if self.input_widget.isVisible(): visible = False else: visible = True self.input_widget.setVisible(visible) def toggle_fullscreen(self): """Toggle fullscreen.""" if self.isFullScreen(): self.showNormal() else: self.showFullScreen() self.change_tab() def linkHover(self, l): """Show link adress in status bar on mouse hover.""" self.statusbar.showMessage(l) def new_tab(self): """Open new tab.""" tasklist = QTreeView() tasklist.hide() tasklist.setObjectName('taskList') tasklist.setMinimumWidth(100) tasklist.setMaximumWidth(250) new_tab = QWebView() new_tab.setObjectName('webView') inspector = QWebInspector(self) inspector.setObjectName('webInspector') inspector.hide() page_layout = QVBoxLayout() page_layout.setSpacing(0) page_layout.setContentsMargins(0, 0, 0, 0) page_layout.addWidget(new_tab) page_layout.addWidget(inspector) page_widget = QFrame() page_widget.setObjectName('pageWidget') page_widget.setLayout(page_layout) complete_tab_layout = QHBoxLayout() complete_tab_layout.setSpacing(0) complete_tab_layout.setContentsMargins(0, 0, 0, 0) complete_tab_layout.addWidget(tasklist) complete_tab_layout.addWidget(page_widget) complete_tab_widget = QFrame() complete_tab_widget.setLayout(complete_tab_layout) new_tab.load(QUrl(self.startpage)) self.tabs.setUpdatesEnabled(False) if self.new_tab_behavior == "insert": self.tabs.insertTab(self.tabs.currentIndex()+1, complete_tab_widget, unicode(new_tab.title())) elif self.new_tab_behavior == "append": self.tabs.appendTab(complete_tab_widget, unicode(new_tab.title())) self.tabs.setCurrentWidget(complete_tab_widget) self.tabs.setTabText(self.tabs.currentIndex(), unicode(self.tabs.currentWidget().findChild(QFrame, unicode('pageWidget')).findChild(QWebView, unicode('webView')).title())) self.tabs.setUpdatesEnabled(True) # tab.page().mainFrame().setScrollBarPolicy(Qt.Horizontal, Qt.ScrollBarAlwaysOff) # tab.page().mainFrame().setScrollBarPolicy(Qt.Vertical, Qt.ScrollBarAlwaysOff) new_tab.titleChanged.connect(self.change_tab) new_tab.urlChanged.connect(self.change_tab) new_tab.loadStarted.connect(self.load_start) new_tab.loadFinished.connect(self.load_finish) new_tab.loadProgress.connect(self.pbar.setValue) new_tab.page().linkHovered.connect(self.linkHover) inspector.setPage(new_tab.page()) def change_tab(self): """Change active tab.""" if self.tabs.count() <= 1: self.tabs.tabBar().hide() else: self.tabs.tabBar().show() try: self.edit.setText(str(self.tabs.currentWidget().findChild(QFrame, unicode('pageWidget')).findChild(QWebView, unicode('webView')).url().toEncoded())) self.tabs.setTabText(self.tabs.currentIndex(), unicode(self.tabs.currentWidget().findChild(QFrame, unicode('pageWidget')).findChild(QWebView, unicode('webView')).title())) self.tabs.currentWidget().findChild(QFrame, unicode('pageWidget')).findChild(QWebView, unicode('webView')).setFocus() except Exception: self.tabs.tabBar().hide() self.new_tab() #print (self.tabs.widget(self.tabs.currentIndex()).size().width()-10), self.tabs.count() self.tabs_widget.setStyleSheet(self.style_sheet + "QTabBar::tab { width:" + str( (self.tabs.widget( self.tabs.currentIndex() ).size().width()-26-self.tabs.count()*2)/self.tabs.count() ) + "px; }") def previous_tab(self): """Previous tab.""" try: if self.tabs.currentIndex() > 0: self.tabs.setCurrentIndex(self.tabs.currentIndex()-1) else: self.tabs.setCurrentIndex(self.tabs.count()-1) self.change_tab() except Exception: pass def next_tab(self): """Next tab.""" try: if self.tabs.currentIndex() < self.tabs.count()-1: self.tabs.setCurrentIndex(self.tabs.currentIndex()+1) else: self.tabs.setCurrentIndex(0) self.change_tab() except Exception: #, e pass def close_tab(self): """Close tab.""" self.tabs.removeTab(self.tabs.currentIndex()) def close(self): """Close app.""" Qapplication.quit() def set_url(self): """Set url.""" url = self.edit.text() # does the url start with http://? if "." not in url: url = "http://www.google.com/search?q="+url elif not url.startswith("http://"): url = "http://" + url qurl = QUrl(url) self.tabs.currentWidget().findChild(QFrame, unicode('pageWidget')).findChild(QWebView, unicode('webView')).load(qurl) self.tabs.currentWidget().findChild(QFrame, unicode('pageWidget')).findChild(QWebView, unicode('webView')).setFocus() def load_start(self): """Update view values, called upon started page load.""" self.rbutton.setText(u"╳") self.rbutton.clicked.connect(self.tabs.currentWidget().findChild(QFrame, unicode('pageWidget')).findChild(QWebView, unicode('webView')).stop) self.pbar.show() def load_finish(self): """Update view values, called upon finished page load.""" if (self.tabs.currentWidget().findChild(QFrame, unicode('pageWidget')).findChild(QWebView, unicode('webView')).history().canGoBack()): self.bbutton.setEnabled(True) else: self.bbutton.setEnabled(False) if (self.tabs.currentWidget().findChild(QFrame, unicode('pageWidget')).findChild(QWebView, unicode('webView')).history().canGoForward()): self.fbutton.setEnabled(True) else: self.fbutton.setEnabled(False) self.rbutton.setText(u"↻") self.rbutton.clicked.connect(self.tabs.currentWidget().findChild(QFrame, unicode('pageWidget')).findChild(QWebView, unicode('webView')).reload) self.pbar.hide() global bookmarks if unicode(self.tabs.currentWidget().findChild(QFrame, unicode('pageWidget')).findChild(QWebView, unicode('webView')).url().toEncoded()) in bookmarks: self.dbutton.setText(u"★") else: self.dbutton.setText(u"☆") if not self.tabs.currentWidget().findChild(QFrame, unicode('pageWidget')).findChild(QWebInspector, unicode('webInspector')).isHidden(): self.tabs.currentWidget().findChild(QFrame, unicode('pageWidget')).findChild(QWebInspector, unicode('webInspector')).hide() self.tabs.currentWidget().findChild(QFrame, unicode('pageWidget')).findChild(QWebInspector, unicode('webInspector')).show() def bookmark(self): """Toggle bookmark.""" global bookmarks if not self.tabs.currentWidget().findChild(QFrame, unicode('pageWidget')).findChild(QWebView, unicode('webView')).url().toEncoded() in bookmarks: bookmarks.append(self.tabs.currentWidget().findChild(QFrame, unicode('pageWidget')).findChild(QWebView, unicode('webView')).url().toEncoded()) pickle.dump(bookmarks, open(bookFile, "wb")) link = QToolButton() link.setDefaultAction(QAction(unicode(unicode(self.tabs.currentWidget().findChild(QFrame, unicode('pageWidget')).findChild(QWebView, unicode('webView')).url().toEncoded())), link)) link.setObjectName(unicode(self.tabs.currentWidget().findChild(QFrame, unicode('pageWidget')).findChild(QWebView, unicode('webView')).url().toEncoded())) link.setFont(QFont("Helvetica Neue", 11, QFont.Normal)) link.clicked.connect(self.handleBookmarks) self.bookmarks_layout.addWidget(link) if self.bookmarks_widget.isHidden(): self.bookmarks_widget.show() self.dbutton.setText(u"★") else: bookmarks.remove(self.tabs.currentWidget().findChild(QFrame, unicode('pageWidget')).findChild(QWebView, unicode('webView')).url().toEncoded()) pickle.dump(bookmarks, open(bookFile, "wb")) link = self.bookmarks_widget.findChild(QToolButton, unicode(self.tabs.currentWidget().findChild(QFrame, unicode('pageWidget')).findChild(QWebView, unicode('webView')).url().toEncoded())) self.bookmarks_layout.removeWidget(link) link.deleteLater() link = None if not bookmarks: self.bookmarks_widget.hide() self.dbutton.setText(u"☆") def handleBookmarks(self): self.gotoLink(self.sender().objectName()) #self.gotoLink(unicode()) def gotoLink(self, url): self.tabs.currentWidget().findChild(QFrame, unicode('pageWidget')).findChild(QWebView, unicode('webView')).load(QUrl(url)) def styleSheet(self, style_sheet): """Load stylesheet.""" try: with open(os.path.join (basedir, 'assets', 'style.qss'), 'r') as file: return file.read() except Exception: # print e return '' def resizeEvent(self, evt=None): """Called on window resize.""" self.change_tab() def myExitHandler(self): """Exiting.""" pass global tabFile # {current_tab: 1, tabs:[0: {current_history:3, history:[{title, url}]]} pb = {'current_tab': self.tabs.currentIndex()} pb['tabs'] = list() for tab in range(self.tabs.count()): pb['tabs'].append(dict(current_history=self.tabs.widget( tab).findChild(QFrame, unicode('pageWidget')).findChild(QWebView, unicode('webView')).history().currentItemIndex(), history=list(dict( title=item.title(), url=item.url() ) for item in self.tabs.widget(tab).findChild(QFrame, unicode('pageWidget')).findChild(QWebView, unicode('webView')).history().items()))) # print pb pickle.dump(pb, open(tabFile, "wb"))
class AboutDialog(QDialog): """ Subclass of `QDialog`_ About Dialog for Embroidermodder. .. sphinx_generate_methods_summary:: AboutDialog """ def __init__(self, parent=None): """ Default class constructor. :param `parent`: Pointer to a parent widget instance. :type `parent`: `QWidget`_ """ super(AboutDialog, self).__init__(parent) p = self.palette() p.setColor(self.backgroundRole(), Qt.white) self.setPalette(p) if parent: self.gImgDir = parent.gImgDir self.gIconDir = parent.gIconDir elif __name__ == '__main__': self.gImgDir = gAppDir + os.sep + 'images' self.gIconDir = gAppDir + os.sep + 'icons' + os.sep + 'default' # The tiled theme background texture. self.bgLogo = QPixmap(self.gImgDir + os.sep + 'texture-spirals.png') self.bgBrush = QBrush(self.bgLogo) self.setWhatsThis( self.tr("""\ The background is a tiled image of an actual design that was stitched out during the pre-alpha stage. It was created by Nina Paley and Theodore Gray using Mathematica in conjunction with our software. They have graciously allowed us to use it for the project in whichever way we wish. We thought it looked so good, that it has become the new theme for Embroidermodder 2. To check out some of the more interesting embroidery projects they are working on, visit http://blog.ninapaley.com/""")) self.imgLbl = EmbroidermodderLogo(self) aboutLbl = QTextBrowser(self) aboutLbl.setReadOnly(True) aboutLbl.setOpenExternalLinks(True) aboutLbl.setText('<b>%s</b>' % '<br>'.join(ABOUT.split('\n'))) aboutLbl.setWhatsThis( self. tr('This is the AWESOME people that brought Embroidermodder 2 to life.' )) # We want very slight opacity of the white background # so the seamless texture shows slightly through. opacityStyleSheet = """\ QTextEdit:read-only { color: rgb(50, 50, 50); font-size: 12px; font-weight: bold; background-color: rgba(255, 255, 255, 240); border: 1px solid rgba(0, 0, 0, 255); } """ aboutLbl.setStyleSheet(opacityStyleSheet) op = QGraphicsOpacityEffect(aboutLbl) op.setOpacity(0.95) aboutLbl.setGraphicsEffect(op) self.notebook = QTabWidget(self) self.notebook.setMinimumWidth(500) self.notebook.addTab(aboutLbl, self.tr('About')) self.notebook.setTabIcon(0, QIcon(self.gIconDir + os.sep + 'app.png')) self.notebook.setTabIcon( 1, QIcon(self.gImgDir + os.sep + 'kickstarter-logo-k-color.png')) notebookStyleSheet = """\ QTabWidget::pane { /* The tab widget frame */ border-top: 1px solid #000000; position: absolute; top: -0.5em; } QTabWidget::tab-bar { alignment: center; } /* Style the tab using the tab sub-control. Note that it reads QTabBar _not_ QTabWidget */ QTabBar::tab { margin-top: 2px; /* make non-selected tabs look smaller */ font-size: 14px; font-weight: bold; background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #E1E1E1, stop: 0.4 #DDDDDD, stop: 0.5 #D8D8D8, stop: 1.0 #D3D3D3); border: 1px solid #000000; /* border-bottom-color: #C2C7CB; */ /* same as the pane color */ border-top-left-radius: 4px; border-top-right-radius: 4px; min-width: 40ex; min-height: 5ex; padding: 3px; } QTabBar::tab:selected { margin-top: 0px; font-size: 16px; font-weight: bold; background: qlineargradient(x1: 0, y1: 0, x2: 2, y2: 2, stop: 0 #0C6AB0, stop: 0.15 #55C4E6, stop: 0.15 #55C4E6, stop: 0.5 #FFFFFF, stop: 0.5 #FFFFFF, stop: 0.85 #55C4E6, stop: 0.85 #55C4E6, stop: 1.0 #0C6AB0); border: 1px solid #000000; } QTabBar::tab:!selected:hover { margin-top: 2px; /* make non-selected tabs look smaller */ font-size: 14px; font-weight: bold; background: qlineargradient(x1: 0, y1: 0, x2: 2, y2: 2, stop: 0 #888888, stop: 0.15 #BBBBBB, stop: 0.15 #BBBBBB, stop: 0.5 #FFFFFF, stop: 0.5 #FFFFFF, stop: 0.85 #BBBBBB, stop: 0.85 #BBBBBB, stop: 1.0 #888888); border: 1px solid #000000; } QTabBar::tab:selected { border-color: #000000; border-bottom-color: #000000; /* same as pane color */ } """ self.notebook.setStyleSheet(notebookStyleSheet) self.notebook.currentChanged.connect(self.CurrentTabChanged) buttonbox = QDialogButtonBox(Qt.Horizontal, self) button = QPushButton(self) button.setText(self.tr("Oh, Yeah!")) button.setWhatsThis( self.tr('This is the Oh, Yeah! button!') + '\n' + self.tr('Oh, Yeah!')) buttonbox.addButton(button, QDialogButtonBox.AcceptRole) buttonbox.setCenterButtons(True) buttonbox.accepted.connect(self.accept) hbLayout1 = QHBoxLayout() hbLayout2 = QHBoxLayout() vbLayout = QVBoxLayout() hbLayout1.addStretch() hbLayout1.addWidget(self.imgLbl) hbLayout1.addStretch() hbLayout2.addStretch() hbLayout2.addWidget(self.notebook) hbLayout2.addStretch() vbLayout.addLayout(hbLayout1) vbLayout.addLayout(hbLayout2) vbLayout.addWidget(buttonbox) self.setLayout(vbLayout) self.setWindowTitle(self.tr('About Embroidermodder Version 2.0')) QApplication.restoreOverrideCursor( ) # TODO/???/PORT# don't mess with the window resize cursors. # def __del__(self): # """Class destructor""" # QApplication.restoreOverrideCursor() def CurrentTabChanged(self): tt = self.notebook.tabText(self.notebook.currentIndex()) if tt == self.tr('About'): self.imgLbl.paintThisText = 'About' elif tt == self.tr('Supporters'): self.imgLbl.paintThisText = 'Kickstarter' self.imgLbl.repaint() def paintEvent(self, event): """ Handles the ``paintEvent`` event for :class:`AboutDialog`. :param `event`: A `QPaintEvent`_ to be processed. """ rect = self.rect() painter = QPainter(self) painter.setRenderHint(painter.SmoothPixmapTransform) # Always fill with a solid color first painter.fillRect(rect, Qt.white) # Then overlay the texture painter.fillRect(rect, self.bgBrush)
class Truss(QMainWindow): def __init__(self, parent=None): super(Truss, self).__init__(parent) self.resize(800, 600) self.filename = None self.filetuple = None self.dirty = False # Refers to Data Page only. centralwidget = QWidget(self) gridLayout = QGridLayout(centralwidget) self.tabWidget = QTabWidget(centralwidget) self.tab = QWidget() font = QFont() font.setFamily("Courier 10 Pitch") font.setPointSize(12) self.tab.setFont(font) gridLayout_3 = QGridLayout(self.tab) self.plainTextEdit = QPlainTextEdit(self.tab) gridLayout_3.addWidget(self.plainTextEdit, 0, 0, 1, 1) self.tabWidget.addTab(self.tab, "") self.tab_2 = QWidget() self.tab_2.setFont(font) gridLayout_2 = QGridLayout(self.tab_2) self.plainTextEdit_2 = QPlainTextEdit(self.tab_2) gridLayout_2.addWidget(self.plainTextEdit_2, 0, 0, 1, 1) self.tabWidget.addTab(self.tab_2, "") gridLayout.addWidget(self.tabWidget, 0, 0, 1, 1) self.setCentralWidget(centralwidget) menubar = QMenuBar(self) menubar.setGeometry(QRect(0, 0, 800, 29)) menu_File = QMenu(menubar) self.menu_Solve = QMenu(menubar) self.menu_Help = QMenu(menubar) self.setMenuBar(menubar) self.statusbar = QStatusBar(self) self.setStatusBar(self.statusbar) self.action_New = QAction(self) self.actionSave_As = QAction(self) self.action_Save = QAction(self) self.action_Open = QAction(self) self.action_Quit = QAction(self) self.action_About = QAction(self) self.actionShow_CCPL = QAction(self) self.action_Solve = QAction(self) self.action_CCPL = QAction(self) self.action_Help = QAction(self) menu_File.addAction(self.action_New) menu_File.addAction(self.action_Open) menu_File.addAction(self.actionSave_As) menu_File.addAction(self.action_Save) menu_File.addSeparator() menu_File.addAction(self.action_Quit) self.menu_Solve.addAction(self.action_Solve) self.menu_Help.addAction(self.action_About) self.menu_Help.addAction(self.action_CCPL) self.menu_Help.addAction(self.action_Help) menubar.addAction(menu_File.menuAction()) menubar.addAction(self.menu_Solve.menuAction()) menubar.addAction(self.menu_Help.menuAction()) self.setWindowTitle("Main Window") self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab),\ "Data Page") self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_2),\ "Solution Page") menu_File.setTitle("&File") self.menu_Solve.setTitle("&Solve") self.menu_Help.setTitle("&Help") self.tabWidget.setCurrentIndex(0) self.action_New.setText("&New") self.action_Open.setText("&Open") self.actionSave_As.setText("Save &As") self.action_Save.setText("&Save") self.action_Quit.setText("&Quit") self.action_Solve.setText("&Solve") self.action_About.setText("&About") self.action_CCPL.setText("&CCPL") self.action_Help.setText("&Help") self.action_Quit.triggered.connect(self.close) allToolBar = self.addToolBar("AllToolBar") allToolBar.setObjectName("AllToolBar") self.addActions(allToolBar, (self.action_Open, self.actionSave_As,\ self.action_Save, self.action_Solve,\ self.action_Quit )) self.action_New.triggered.connect(self.fileNew) self.action_Open.triggered.connect(self.fileOpen) self.actionSave_As.triggered.connect(self.fileSaveAs) self.action_Save.triggered.connect(self.fileSave) self.action_Solve.triggered.connect(self.trussSolve) self.action_About.triggered.connect(self.aboutBox) self.action_CCPL.triggered.connect(self.displayCCPL) self.action_Help.triggered.connect(self.help) self.plainTextEdit.textChanged.connect(self.setDirty) self.action_New = self.editAction(self.action_New, None,\ 'ctrl+N', 'filenew', 'New File.') self.action_Open = self.editAction(self.action_Open, None, 'ctrl+O', 'fileopen', 'Open File.') self.actionSave_As = self.editAction(self.actionSave_As,\ None, 'ctrl+A', 'filesaveas',\ 'Save and Name File.') self.action_Save = self.editAction(self.action_Save, None, 'ctrl+S', 'filesave', 'Save File.') self.action_Solve = self.editAction(self.action_Solve, None, 'ctrl+L', 'solve', 'Solve Structure.') self.action_About = self.editAction(self.action_About, None, 'ctrl+B', 'about', 'Pop About Box.') self.action_CCPL = self.editAction(self.action_CCPL, None, 'ctrl+G', 'licence', 'Show Licence') self.action_Help = self.editAction(self.action_Help, None, 'ctrl+H', 'help', 'Show Help Page.') self.action_Quit = self.editAction(self.action_Quit, None, 'ctrl+Q', 'quit', 'Quit the program.') self.plainTextEdit_2.setReadOnly(True) def setDirty(self): '''On change of text in textEdit window, set the flag "dirty" to True''' index = self.tabWidget.currentIndex() if index is not 0: return if self.dirty: return True self.dirty = True self.updateStatus('self.dirty set to True') def clearDirty(self): 'Clear dirty flag' self.dirty = False def fileNew(self): '''Clear both Data Page and Solution Page.''' self.plainTextEdit.setPlainText(' ') self.plainTextEdit_2.setPlainText(' ') self.clearDirty(self) def okToContinue(self): if self.dirty: reply = QMessageBox.question( self, "Data Loader - Unsaved Changes", "Save unsaved changes?", QMessageBox.Yes | QMessageBox.No | QMessageBox.Cancel) if reply == QMessageBox.Cancel: return False elif reply == QMessageBox.Yes: self.clearDirty() return self.fileSave() return True def okRead(self): 'Pop-up a warning message.' reply = QMessageBox.warning( self, "Warning", '''\nFile Open and Save is possible only in Data Page! \n\(Use SaveAs for Solution Page)''', QMessageBox.Ok) return True def fileOpen(self): '''Open a file in Data Page (with index == 0)''' if self.tabWidget.currentIndex(): self.okRead() return if not self.okToContinue(): return dir = (os.path.dirname(self.filename) if self.filename is not None else ".") self.filetuple = QFileDialog.getOpenFileName(self,\ "Open File", dir, \ "Data (*.dat *.txt)\nAll Files (*.*)") self.filename = self.filetuple[0] fname = self.filename # QFileDialog returns a tuple x with x[0] = file name and # x[1] = type of filter. if fname: self.loadFile(fname) self.filename = fname self.updateStatus('New file opened.') def loadFile(self, fname=None): fl = open(fname) text = fl.read() self.plainTextEdit.setPlainText(text) self.dirty = False def fileSave(self): '''Save file with current file name.''' if self.tabWidget.currentIndex(): self.okRead() return if self.filename is None: return self.fileSaveAs() else: flname = self.filename if flname: try: with open(flname, 'w') as fl: fl.write(tempText) self.dirty = False self.updateStatus('File saved.') except IOError: self.dirty = True self.updateStatus('File not saved.') return False return True else: self.updateStatus('Failed to save... ') return False self.filename = None self.dirty = False def fileSaveAs(self): '''Save file with a new name.''' qpr = self.qprintline fname = self.filename if self.filename is not None else\ "NoName" self.filetuple = QFileDialog.getSaveFileName( self, "Truss program - Save File", fname, "Data File (*.*)") flname = self.filetuple[0] index = self.tabWidget.currentIndex() if index == 0: self.filename = flname if flname: fl = open(flname, 'w') tempText = self.plainTextEdit.toPlainText() fl.write(tempText) fl.close() self.dirty = False self.updateStatus('File saved.') elif index == 1: if flname: fl = open(flname, 'w') tempText = self.plainTextEdit_2.toPlainText() fl.write(tempText) fl.close() def trussSolve(self): '''Solve a statically determinate truss, specified in Data Page and display the results in the Solution Page. To start, make a copy of the Data Page with a header all shown on the Data Page.''' printline = self.qprintline dataBall = self.plainTextEdit.toPlainText() self.plainTextEdit_2.clear() printline('================================') flbase = os.path.basename(self.filename) printline('SOLUTION FOR ' + flbase) printline('================================') dataBall = self.plainTextEdit.toPlainText() ncrunch.main(printline, self.filename, dataBall) def aboutBox(self): '''Popup a box with about message.''' QMessageBox.about(self, "About PySide, Platform and the like", """<b>Part of Structural Analysis.</b> v %s <p>Copyright © 2011 Algis Kabaila. All rights reserved in accordance with Creative Commons Attribution Licence (CCPL) v3 or later - NO WARRANTIES! <p>This progam finds bar forces in statically determinate trusses. <p>Python %s - PySide version %s - Qt version %s on\ %s""" % (__version__, platform.python_version(),\ PySide.__version__, PySide.QtCore.__version__, platform.system())) def displayCCPL(self): '''Read and display CCPL licence.''' self.plainTextEdit.setPlainText(open('CCPL.txt').read()) self.dirty = False self.filename = 'COPYING.txt' self.updateStatus('CCPL displayed.') def help(self): '''Read and display a help file- currently the README.txt.''' self.plainTextEdit.setPlainText(open('README.md').read()) self.dirty = False self.filename = 'README.txt' self.updateStatus('README displayed.') def addActions(self, target, actions): '''Actions are added to Tool Bar.''' for action in actions: if action is None: target.addSeparator() else: target.addAction(action) def editAction(self, action, slot=None, shortcut=None, icon=None, tip=None): '''This method adds to action: icon, shortcut, ToolTip,\ StatusTip and can connect triggered action to slot ''' if icon is not None: action.setIcon(QIcon(":/%s.png" % (icon))) if shortcut is not None: action.setShortcut(shortcut) if tip is not None: action.setToolTip(tip) action.setStatusTip(tip) if slot is not None: action.triggered.connect(slot) return action def qreadline(self, lineNo): '''Read one line from Data Page (lineNo starts with 0)''' return str(self.plainTextEdit.document().\ findBlockByLineNumber(lineNo).text()).rstrip() def qprintline(self, line): '''Append one line to Solution Page.''' self.plainTextEdit_2.appendPlainText(line.rstrip()) def updateStatus(self, message): '''Keep status current.''' if self.filename is not None: flbase = os.path.basename(self.filename) self.setWindowTitle(str("Truss Analysis - " +\ flbase + "[*]") ) self.statusBar().showMessage(message, 5000) self.setWindowModified(self.dirty)
class StartView(QMainWindow): def __init__(self): super(StartView, self).__init__() self.setWindowTitle("hello") self.resize(1024, 768) # Menu main_menu = self.menuBar() self.create = main_menu.addMenu('&Create') self.run = main_menu.addMenu('&Run') self.report = main_menu.addMenu('&Report') self.help = main_menu.addMenu('&Help') action_batch = self.create.addAction('&Batch') action_case = self.create.addAction('&Case') action_data = self.create.addAction('&Data') action_web = self.create.addAction('&Web') action_run = self.run.addAction('&Run') action_test = self.run.addAction('&Test') action_report = self.report.addAction('&Report') self.connect(action_batch, SIGNAL('triggered()'), self.open_batch) self.connect(action_case, SIGNAL('triggered()'), self.open_case) self.connect(action_test, SIGNAL('triggered()'), self.open_test) self.connect(action_data, SIGNAL('triggered()'), self.open_data) self.connect(action_web, SIGNAL('triggered()'), self.open_web_object) self.connect(action_run, SIGNAL('triggered()'), self.open_run) self.connect(action_report, SIGNAL('triggered()'), self.open_report) # Dock self.dock_category = DockCategory() # category widget self.dock_log = DockBottom() # log widget self.dock_detail = DockDetail() # detail widget self.__dock_displayed = False # center widget self.__wid_center = QTabWidget() self.setCentralWidget(self.__wid_center) self.__wid_center.setStyleSheet(get_theme("TabViewMain")) self.__wid_center.setTabsClosable(True) self.connect(self.__wid_center, SIGNAL("tabCloseRequested(int)"), self.close_tab) def close_tab(self): index = self.__wid_center.currentIndex() self.__wid_center.removeTab(index) def open_data(self): _view = ViewDataMag() self.__add_tab(_view) def open_batch(self): _view = ViewBatchDefMag() _view.sig_batch_det[dict].connect(self.open_case_select) self.__add_tab(_view) def open_case(self): _view = ViewCaseDefMag() _view.sig_case_det[dict].connect(self.open_step) self.__add_tab(_view) def open_case_select(self, p_data): _view = ViewBatchDetMag(p_data) self.__add_tab(_view) def open_step(self, p_data): _view = StepContainer(p_data) self.__add_tab(_view) def open_web_object(self): _view = ViewWebMain() self.__add_tab(_view) def open_run(self): _view = ViewRunMain() self.__add_tab(_view) def open_report(self): _view = ViewReportMain() self.__add_tab(_view) def open_test(self): self.__show_dock() def __add_tab(self, p_view): self.__wid_center.addTab(p_view, p_view.title) self.__wid_center.setCurrentWidget(p_view) def __show_dock(self): if not self.__dock_displayed: self.addDockWidget(Qt.BottomDockWidgetArea, self.dock_log) # self.addDockWidget(Qt.LeftDockWidgetArea, self.dock_category) # self.addDockWidget(Qt.RightDockWidgetArea, self.dock_detail) pass
class Truss(QMainWindow): def __init__(self, parent=None): super(Truss, self).__init__(parent) self.resize(800, 600) self.filename = None self.filetuple = None self.dirty = False # Refers to Data Page only. centralwidget = QWidget(self) gridLayout = QGridLayout(centralwidget) self.tabWidget = QTabWidget(centralwidget) self.tab = QWidget() font = QFont() font.setFamily("Courier 10 Pitch") font.setPointSize(12) self.tab.setFont(font) gridLayout_3 = QGridLayout(self.tab) self.plainTextEdit = QPlainTextEdit(self.tab) gridLayout_3.addWidget(self.plainTextEdit, 0, 0, 1, 1) self.tabWidget.addTab(self.tab, "") self.tab_2 = QWidget() self.tab_2.setFont(font) gridLayout_2 = QGridLayout(self.tab_2) self.plainTextEdit_2 = QPlainTextEdit(self.tab_2) gridLayout_2.addWidget(self.plainTextEdit_2, 0, 0, 1, 1) self.tabWidget.addTab(self.tab_2, "") gridLayout.addWidget(self.tabWidget, 0, 0, 1, 1) self.setCentralWidget(centralwidget) menubar = QMenuBar(self) menubar.setGeometry(QRect(0, 0, 800, 29)) menu_File = QMenu(menubar) self.menu_Solve = QMenu(menubar) self.menu_Help = QMenu(menubar) self.setMenuBar(menubar) self.statusbar = QStatusBar(self) self.setStatusBar(self.statusbar) self.action_New = QAction(self) self.actionSave_As = QAction(self) self.action_Save = QAction(self) self.action_Open = QAction(self) self.action_Quit = QAction(self) self.action_About = QAction(self) self.actionShow_CCPL = QAction(self) self.action_Solve = QAction(self) self.action_CCPL = QAction(self) self.action_Help = QAction(self) menu_File.addAction(self.action_New) menu_File.addAction(self.action_Open) menu_File.addAction(self.actionSave_As) menu_File.addAction(self.action_Save) menu_File.addSeparator() menu_File.addAction(self.action_Quit) self.menu_Solve.addAction(self.action_Solve) self.menu_Help.addAction(self.action_About) self.menu_Help.addAction(self.action_CCPL) self.menu_Help.addAction(self.action_Help) menubar.addAction(menu_File.menuAction()) menubar.addAction(self.menu_Solve.menuAction()) menubar.addAction(self.menu_Help.menuAction()) self.setWindowTitle("Main Window") self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab),\ "Data Page") self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_2),\ "Solution Page") menu_File.setTitle("&File") self.menu_Solve.setTitle("&Solve") self.menu_Help.setTitle("&Help") self.tabWidget.setCurrentIndex(0) self.action_New.setText("&New") self.action_Open.setText("&Open") self.actionSave_As.setText("Save &As") self.action_Save.setText("&Save") self.action_Quit.setText("&Quit") self.action_Solve.setText("&Solve") self.action_About.setText("&About") self.action_CCPL.setText("&CCPL") self.action_Help.setText("&Help") self.action_Quit.triggered.connect(self.close) allToolBar = self.addToolBar("AllToolBar") allToolBar.setObjectName("AllToolBar") self.addActions(allToolBar, (self.action_Open, self.actionSave_As,\ self.action_Save, self.action_Solve,\ self.action_Quit )) self.action_New.triggered.connect(self.fileNew) self.action_Open.triggered.connect(self.fileOpen) self.actionSave_As.triggered.connect(self.fileSaveAs) self.action_Save.triggered.connect(self.fileSave) self.action_Solve.triggered.connect(self.trussSolve) self.action_About.triggered.connect(self.aboutBox) self.action_CCPL.triggered.connect(self.displayCCPL) self.action_Help.triggered.connect(self.help) self.plainTextEdit.textChanged.connect(self.setDirty) self.action_New = self.editAction(self.action_New, None,\ 'ctrl+N', 'filenew', 'New File.') self.action_Open = self.editAction(self.action_Open, None, 'ctrl+O', 'fileopen', 'Open File.') self.actionSave_As = self.editAction(self.actionSave_As,\ None, 'ctrl+A', 'filesaveas',\ 'Save and Name File.') self.action_Save = self.editAction(self.action_Save, None, 'ctrl+S', 'filesave', 'Save File.') self.action_Solve = self.editAction(self.action_Solve, None, 'ctrl+L', 'solve', 'Solve Structure.') self.action_About = self.editAction(self.action_About, None, 'ctrl+B', 'about','Pop About Box.') self.action_CCPL = self.editAction(self.action_CCPL, None, 'ctrl+G', 'licence', 'Show Licence') self.action_Help = self.editAction(self.action_Help, None, 'ctrl+H', 'help', 'Show Help Page.') self.action_Quit = self.editAction(self.action_Quit, None, 'ctrl+Q', 'quit', 'Quit the program.') self.plainTextEdit_2.setReadOnly(True) def setDirty(self): '''On change of text in textEdit window, set the flag "dirty" to True''' index = self.tabWidget.currentIndex() if index is not 0: return if self.dirty: return True self.dirty = True self.updateStatus('self.dirty set to True') def clearDirty(self): 'Clear dirty flag' self.dirty = False def fileNew(self): '''Clear both Data Page and Solution Page.''' self.plainTextEdit.setPlainText(' ') self.plainTextEdit_2.setPlainText(' ') self.clearDirty(self) def okToContinue(self): if self.dirty: reply = QMessageBox.question(self, "Data Loader - Unsaved Changes", "Save unsaved changes?", QMessageBox.Yes|QMessageBox.No|QMessageBox.Cancel) if reply == QMessageBox.Cancel: return False elif reply == QMessageBox.Yes: self.clearDirty() return self.fileSave() return True def okRead(self): 'Pop-up a warning message.' reply = QMessageBox.warning(self, "Warning", '''\nFile Open and Save is possible only in Data Page! \n\(Use SaveAs for Solution Page)''', QMessageBox.Ok) return True def fileOpen(self): '''Open a file in Data Page (with index == 0)''' if self.tabWidget.currentIndex(): self.okRead() return if not self.okToContinue(): return dir = (os.path.dirname(self.filename) if self.filename is not None else ".") self.filetuple = QFileDialog.getOpenFileName(self,\ "Open File", dir, \ "Data (*.dat *.txt)\nAll Files (*.*)") self.filename = self.filetuple[0] fname = self.filename # QFileDialog returns a tuple x with x[0] = file name and # x[1] = type of filter. if fname: self.loadFile(fname) self.filename = fname self.updateStatus('New file opened.') def loadFile(self, fname=None): fl = open(fname) text = fl.read() self.plainTextEdit.setPlainText(text) self.dirty = False def fileSave(self): '''Save file with current file name.''' if self.tabWidget.currentIndex(): self.okRead() return if self.filename is None: return self.fileSaveAs() else: flname = self.filename if flname: with open(flname, 'w') as fl: fl.write(tempText) self.dirty = False self.updateStatus('File saved.') return True else: self.updateStatus('Failed to save... ') return False self.filename = None self.dirty = False def fileSaveAs(self): '''Save file with a new name.''' qpr = self.qprintline fname = self.filename if self.filename is not None else\ "NoName" self.filetuple = QFileDialog.getSaveFileName(self, "Truss program - Save File", fname, "Data File (*.*)") flname = self.filetuple[0] index = self.tabWidget.currentIndex() if index == 0: self.filename = flname if flname: fl = open(flname, 'w') tempText = self.plainTextEdit.toPlainText() fl.write(tempText) fl.close() self.dirty = False self.updateStatus('File saved.') elif index == 1: if flname: fl = open(flname, 'w') tempText = self.plainTextEdit_2.toPlainText() fl.write(tempText) fl.close() def trussSolve(self): '''Solve a statically determinate truss, specified in Data Page and display the results in the Solution Page. To start, make a copy of the Data Page with a header all shown on the Data Page.''' printline = self.qprintline dataBall = self.plainTextEdit.toPlainText() self.plainTextEdit_2.clear() printline('================================') flbase = os.path.basename(self.filename) printline('SOLUTION FOR ' + flbase) printline('================================') dataBall = self.plainTextEdit.toPlainText() ncrunch.main(printline, self.filename, dataBall) def aboutBox(self): '''Popup a box with about message.''' QMessageBox.about(self, "About PySide, Platform and the like", """<b>Part of Structural Analysis.</b> v %s <p>Copyright © 2011 Algis Kabaila. All rights reserved in accordance with Creative Commons Attribution Licence (CCPL) v3 or later - NO WARRANTIES! <p>This progam finds bar forces in statically determinate trusses. <p>Python %s - PySide version %s - Qt version %s on\ %s""" % (__version__, platform.python_version(),\ PySide.__version__, PySide.QtCore.__version__, platform.system())) def displayCCPL(self): '''Read and display CCPL licence.''' self.plainTextEdit.setPlainText(open('CCPL.txt').read()) self.dirty = False self.filename = 'COPYING.txt' self.updateStatus('CCPL displayed.') def help(self): '''Read and display a help file- currently the README.txt.''' self.plainTextEdit.setPlainText(open('README.md').read()) self.dirty = False self.filename = 'README.txt' self.updateStatus('README displayed.') def addActions(self, target, actions): '''Actions are added to Tool Bar.''' for action in actions: if action is None: target.addSeparator() else: target.addAction(action) def editAction(self, action, slot=None, shortcut=None, icon=None, tip=None): '''This method adds to action: icon, shortcut, ToolTip,\ StatusTip and can connect triggered action to slot ''' if icon is not None: action.setIcon(QIcon(":/%s.png" % (icon))) if shortcut is not None: action.setShortcut(shortcut) if tip is not None: action.setToolTip(tip) action.setStatusTip(tip) if slot is not None: action.triggered.connect(slot) return action def qreadline(self, lineNo): '''Read one line from Data Page (lineNo starts with 0)''' return unicode(self.plainTextEdit.document().\ findBlockByLineNumber(lineNo).text()).rstrip() def qprintline(self, line): '''Append one line to Solution Page.''' self.plainTextEdit_2.appendPlainText(line.rstrip()) def updateStatus(self, message): '''Keep status current.''' if self.filename is not None: flbase = os.path.basename(self.filename) self.setWindowTitle(unicode("Truss Analysis - " +\ flbase + "[*]") ) self.statusBar().showMessage(message, 5000) self.setWindowModified(self.dirty)