def _on_context_menu(self, point=None): selection = normalize(unicode(self.textCursor().selectedText())) word = self.highlight.get_word_of_position(self.cursorForPosition(point).position()) if word is None: if not self.textCursor().hasSelection(): return # highlight selected word from NewWordEditor import create_new_word nw = create_new_word(self, selection) if nw is None: return nw.article_id = self.article_id s = get_session() s.add(nw) s.commit() cursor = self.textCursor() cursor.clearSelection() self.setTextCursor(cursor) else: s = get_session() s.delete(ArticleNewWord.find_by_article_word(s, self.article_id, word)) s.commit() self._refresh()
def get_new_word(self): s = get_session() w = unicode(self.word_edit.text()).strip() word = Word.find(s, w) if word is not None: word = word.id else: word = Word(w) return ArticleNewWord(None, word, unicode(self.content_edit.text()).strip())
def _show_article_list(self, window): from ljn.Model import Category from ljn.Repository import get_session id = self._get_selected_category_id(window) if id is not None: msg = ' (%s)' % Category.find_by_id(get_session(), id).name else: msg = '' window.list_dock_pane.setWindowTitle("Article List" + msg) window.list_layout.setCurrentWidget(window.article_list)
def _new_article(self): article = ArticleEditor.create_new_article(self) if article is None: return article.category_id = self.category_id s = get_session() s.add(article) s.commit() self.update_articles() self.onArticleCreated.emit(article.id)
def _new_category(self): text, result = QInputDialog.getText(self, 'New category', 'New category name:') if not result: return text = str(text) if not text: return s = get_session() s.add(Category(text)) s.commit() self.update_categories()
def update_words(self, article_id=None): self.clear() if article_id is None: article_id = self.article_id if article_id is None: return self.article_id = article_id s = get_session() self.new_words = ArticleNewWord.all_article_new_word(s, article_id) for nw in self.new_words: self.addItem(WordItem(nw)) self.sortItems()
def del_word(self, window, article_id, word_content): word_content = unicode(word_content) msg = 'Delete "%s"?' % word_content btn = QMessageBox.question(window, 'Delete word', msg, QMessageBox.Yes | QMessageBox.No) if btn != QMessageBox.Yes: return from ljn.Repository import get_session s = get_session() anw = ArticleNewWord.find_by_article_word(s, article_id, word_content) if anw is not None: s.delete(anw) s.commit() self._update_word_list(window, article_id)
def _del_category(self): items = self.selectedItems() if not items: return category = items[0].category msg = 'Delete "%s"?' % category.name btn = QMessageBox.question(self, 'Delete category', msg, QMessageBox.Yes | QMessageBox.No) if btn == QMessageBox.No: return s = get_session() s.delete(Category.find_by_id(s, category.id)) s.commit() self.update_categories()
def _del_article(self): items = self.selectedItems() if not items: return article = items[0].article msg = 'Delete "%s"?' % article.title btn = QMessageBox.question(self, 'Delete article', msg, QMessageBox.Yes | QMessageBox.No) if btn == QMessageBox.No: return s = get_session() s.delete(Article.find_by_id(s, article.id)) s.commit() self.update_articles()
def update_articles(self, category_id=None): self.clear() self.addItem('..') if category_id is None: category_id = self.category_id if category_id is None: return self.category_id = category_id category = Category.find_by_id(get_session(), category_id) if category is not None: for a in category.articles: self.addItem(ArticleItem(a))
def _rename_category(self): items = self.selectedItems() if not items: return category = items[0].category text, result = QInputDialog.getText(self, 'Rename category', 'New category name:', text=category.name) if not result: return text = str(text) if not text or text == category.name: return s = get_session() c = Category.find_by_id(s, category.id) c.name = text s.commit() self.update_categories()
def _rename_article(self): items = self.selectedItems() if not items: return article = items[0].article text, result = QInputDialog.getText(self, 'Rename article', 'New article title:', text=article.title) if not result: return text = str(text) if not text or text == article.title: return s = get_session() a = Article.find_by_id(s, article.id) a.title = text s.commit() self.update_articles()
def _refresh(self): article = Article.find_by_id(get_session(), self.article_id) self.new_words = list(article.new_words) self.highlight.rehighlight() self.onArticleLoaded.emit(self.article_id)
def open_article(self, window, article_id): from ljn.Model import Article from ljn.Repository import get_session window.article_browser.setFocus() window.article_browser.set_article(Article.find_by_id(get_session(), article_id))
def update_categories(self): self.clear() for c in Category.all(get_session()): self.addItem(CategoryItem(c))