2024-09-01 02:25:22 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-09-07 01:53:48 +00:00
|
|
|
"embed"
|
2024-09-01 02:25:22 +00:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2024-09-07 01:53:48 +00:00
|
|
|
"path/filepath"
|
2024-09-01 02:25:22 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/mappu/miqt/qt"
|
|
|
|
)
|
|
|
|
|
2024-09-07 01:53:48 +00:00
|
|
|
//go:embed README.md
|
|
|
|
var embedContent embed.FS
|
2024-09-01 02:25:22 +00:00
|
|
|
|
2024-09-07 01:53:48 +00:00
|
|
|
const (
|
|
|
|
lineNumberRole = int(qt.ItemDataRole__UserRole + 1)
|
|
|
|
)
|
2024-09-01 02:25:22 +00:00
|
|
|
|
2024-09-07 01:53:48 +00:00
|
|
|
type AppWindow struct {
|
|
|
|
w *qt.QMainWindow
|
|
|
|
cw *qt.QWidget
|
2024-09-01 02:25:22 +00:00
|
|
|
|
2024-09-07 01:58:14 +00:00
|
|
|
tabs *qt.QTabWidget
|
2024-09-07 01:53:48 +00:00
|
|
|
}
|
2024-09-01 02:25:22 +00:00
|
|
|
|
2024-09-07 01:53:48 +00:00
|
|
|
type AppTab struct {
|
|
|
|
tab *qt.QWidget
|
2024-09-01 02:25:22 +00:00
|
|
|
outline *qt.QListWidget
|
|
|
|
textArea *qt.QTextEdit
|
|
|
|
}
|
|
|
|
|
2024-09-07 01:53:48 +00:00
|
|
|
func NewAppTab() *AppTab {
|
|
|
|
var ret AppTab
|
|
|
|
|
|
|
|
tab := qt.NewQWidget()
|
|
|
|
ret.tab = tab
|
|
|
|
|
|
|
|
layout := qt.NewQHBoxLayout2(tab)
|
|
|
|
|
|
|
|
panes := qt.NewQSplitter()
|
|
|
|
layout.AddWidget(panes.QWidget)
|
|
|
|
|
|
|
|
ret.outline = qt.NewQListWidget2(tab)
|
|
|
|
panes.AddWidget(ret.outline.QWidget)
|
|
|
|
ret.outline.OnCurrentItemChanged(ret.handleJumpToBookmark)
|
|
|
|
|
|
|
|
ret.textArea = qt.NewQTextEdit3(tab)
|
|
|
|
ret.textArea.OnTextChanged(ret.handleTextChanged)
|
|
|
|
|
|
|
|
panes.AddWidget(ret.textArea.QWidget)
|
|
|
|
|
|
|
|
panes.SetSizes([]int{250, 550})
|
|
|
|
|
|
|
|
return &ret
|
|
|
|
}
|
|
|
|
|
2024-09-01 02:25:22 +00:00
|
|
|
func NewAppWindow() *AppWindow {
|
|
|
|
|
|
|
|
ret := AppWindow{}
|
|
|
|
|
|
|
|
ret.w = qt.NewQMainWindow()
|
|
|
|
ret.w.SetWindowTitle("Markdown Outliner")
|
|
|
|
|
|
|
|
// Menu
|
|
|
|
|
|
|
|
mnu := qt.NewQMenuBar()
|
|
|
|
fileMenu := mnu.AddMenuWithTitle("&File")
|
2024-09-07 01:53:48 +00:00
|
|
|
|
|
|
|
newtab := fileMenu.AddAction("New Tab")
|
|
|
|
newtab.SetShortcut(qt.NewQKeySequence2("Ctrl+N"))
|
|
|
|
newtab.SetIcon(qt.QIcon_FromTheme("document-new"))
|
2024-09-07 03:31:11 +00:00
|
|
|
newtab.OnTriggered(func() {
|
2024-09-07 01:53:48 +00:00
|
|
|
ret.createTabWithContents("New Document", "")
|
|
|
|
})
|
|
|
|
|
2024-09-01 02:25:22 +00:00
|
|
|
open := fileMenu.AddAction("Open...")
|
|
|
|
open.SetShortcut(qt.NewQKeySequence2("Ctrl+O"))
|
2024-09-07 01:37:00 +00:00
|
|
|
open.SetIcon(qt.QIcon_FromTheme("document-open"))
|
2024-09-07 03:31:11 +00:00
|
|
|
open.OnTriggered(ret.handleFileOpen)
|
2024-09-07 01:37:00 +00:00
|
|
|
|
2024-09-01 02:25:22 +00:00
|
|
|
fileMenu.AddSeparator()
|
|
|
|
exit := fileMenu.AddAction("Exit")
|
2024-09-07 01:37:00 +00:00
|
|
|
exit.SetIcon(qt.QIcon_FromTheme("application-exit"))
|
2024-09-07 03:31:11 +00:00
|
|
|
exit.OnTriggered(func() {
|
2024-09-01 02:25:22 +00:00
|
|
|
os.Exit(0)
|
|
|
|
})
|
|
|
|
|
|
|
|
helpMenu := mnu.AddMenuWithTitle("&Help")
|
|
|
|
aboutQt := helpMenu.AddAction("About Qt")
|
2024-09-07 01:37:00 +00:00
|
|
|
aboutQt.SetIcon(qt.QIcon_FromTheme("help-about"))
|
2024-09-01 02:25:22 +00:00
|
|
|
aboutQt.SetShortcut(qt.NewQKeySequence2("F1"))
|
2024-09-07 03:31:11 +00:00
|
|
|
aboutQt.OnTriggered(func() {
|
2024-09-01 02:25:22 +00:00
|
|
|
qt.QApplication_AboutQt()
|
|
|
|
})
|
|
|
|
ret.w.SetMenuBar(mnu)
|
|
|
|
|
|
|
|
// Main widgets
|
|
|
|
|
2024-09-07 01:53:48 +00:00
|
|
|
ret.tabs = qt.NewQTabWidget2(ret.w.QWidget)
|
2024-09-07 01:58:14 +00:00
|
|
|
ret.tabs.SetTabsClosable(true)
|
2024-09-14 22:31:00 +00:00
|
|
|
ret.tabs.OnTabCloseRequested(func(index int) {
|
|
|
|
ret.handleTabClose(index)
|
2024-09-07 01:58:14 +00:00
|
|
|
})
|
|
|
|
|
2024-09-07 01:53:48 +00:00
|
|
|
ret.w.SetCentralWidget(ret.tabs.QWidget)
|
2024-09-01 02:25:22 +00:00
|
|
|
|
2024-09-07 01:53:48 +00:00
|
|
|
// Add initial tab
|
2024-09-01 02:25:22 +00:00
|
|
|
|
2024-09-07 01:53:48 +00:00
|
|
|
sampleContent, err := embedContent.ReadFile("README.md")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
ret.createTabWithContents("README.md", string(sampleContent))
|
2024-09-01 02:25:22 +00:00
|
|
|
|
|
|
|
return &ret
|
|
|
|
}
|
|
|
|
|
2024-09-07 01:58:14 +00:00
|
|
|
func (a *AppWindow) handleTabClose(tabIndex int) {
|
|
|
|
a.tabs.RemoveTab(tabIndex)
|
|
|
|
}
|
|
|
|
|
2024-09-01 02:25:22 +00:00
|
|
|
func (a *AppWindow) handleFileOpen() {
|
|
|
|
fname := qt.QFileDialog_GetOpenFileName4(a.w.QWidget, "Open markdown file...", "", "Markdown files (*.md *.txt);;All Files (*)")
|
|
|
|
if fname == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
contents, err := ioutil.ReadFile(fname)
|
|
|
|
if err != nil {
|
|
|
|
qt.QMessageBox_Warning(a.w.QWidget, "Failed to open file", fmt.Sprintf("Opening file %q: %v", fname, err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-09-07 01:53:48 +00:00
|
|
|
a.createTabWithContents(filepath.Base(fname), string(contents))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AppWindow) createTabWithContents(tabTitle, tabContent string) {
|
|
|
|
|
|
|
|
tab := NewAppTab()
|
|
|
|
tab.textArea.SetText(tabContent)
|
|
|
|
|
|
|
|
tabIdx := a.tabs.AddTab2(tab.tab, qt.QIcon_FromTheme("text-markdown"), tabTitle)
|
|
|
|
a.tabs.SetCurrentIndex(tabIdx)
|
2024-09-01 02:25:22 +00:00
|
|
|
}
|
|
|
|
|
2024-09-07 01:53:48 +00:00
|
|
|
func (t *AppTab) handleTextChanged() {
|
|
|
|
content := t.textArea.ToPlainText()
|
|
|
|
t.updateOutlineForContent(content)
|
2024-09-01 02:25:22 +00:00
|
|
|
}
|
|
|
|
|
2024-09-07 01:53:48 +00:00
|
|
|
func (t *AppTab) updateOutlineForContent(content string) {
|
|
|
|
t.outline.Clear()
|
2024-09-01 02:25:22 +00:00
|
|
|
|
|
|
|
lines := strings.Split(content, "\n")
|
|
|
|
|
|
|
|
for lineNumber, line := range lines {
|
|
|
|
if strings.HasPrefix(line, `#`) {
|
|
|
|
|
2024-09-07 01:53:48 +00:00
|
|
|
bookmark := qt.NewQListWidgetItem7(line, t.outline)
|
2024-09-07 01:37:00 +00:00
|
|
|
bookmark.SetToolTip(fmt.Sprintf("Line %d", lineNumber+1))
|
2024-09-01 02:25:22 +00:00
|
|
|
bookmark.SetData(lineNumberRole, qt.NewQVariant5(lineNumber))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-14 22:31:00 +00:00
|
|
|
func (t *AppTab) handleJumpToBookmark(current *qt.QListWidgetItem, previous *qt.QListWidgetItem) {
|
2024-09-07 01:53:48 +00:00
|
|
|
itm := t.outline.CurrentItem()
|
2024-09-01 02:25:22 +00:00
|
|
|
if itm == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
lineNumber := itm.Data(lineNumberRole).ToInt()
|
|
|
|
|
2024-09-07 01:53:48 +00:00
|
|
|
targetBlock := t.textArea.Document().FindBlockByLineNumber(lineNumber)
|
|
|
|
t.textArea.SetTextCursor(qt.NewQTextCursor4(targetBlock))
|
2024-09-01 02:25:22 +00:00
|
|
|
|
2024-09-07 01:53:48 +00:00
|
|
|
t.textArea.SetFocus()
|
2024-09-01 02:25:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
qt.NewQApplication(os.Args)
|
|
|
|
|
|
|
|
app := NewAppWindow()
|
|
|
|
app.w.Show()
|
|
|
|
|
|
|
|
qt.QApplication_Exec()
|
|
|
|
}
|