├── .clang-format ├── .editorconfig ├── .github └── workflows │ ├── build_test.yml │ └── check_format.yml ├── .gitignore ├── CMakeLists.txt ├── LICENSE.MIT ├── README.md ├── example ├── CMakeLists.txt ├── image │ └── preview.png ├── include │ └── MainWindow.hpp ├── resources │ ├── code_samples │ │ ├── cxx.cpp │ │ ├── java.java │ │ ├── js.js │ │ ├── json.json │ │ ├── lua.lua │ │ ├── python.py │ │ ├── shader.glsl │ │ └── xml.xml │ ├── demo_resources.qrc │ └── styles │ │ └── drakula.xml └── src │ ├── MainWindow.cpp │ └── main.cpp ├── include ├── QCXXHighlighter ├── QCodeEditor ├── QGLSLCompleter ├── QGLSLHighlighter ├── QHighlightBlockRule ├── QHighlightRule ├── QJSHighlighter ├── QJSONHighlighter ├── QJavaHighlighter ├── QLanguage ├── QLineNumberArea ├── QLuaCompleter ├── QLuaHighlighter ├── QPythonCompleter ├── QPythonHighlighter ├── QStyleSyntaxHighlighter ├── QSyntaxStyle ├── QXMLHighlighter └── internal │ ├── QCXXHighlighter.hpp │ ├── QCodeEditor.hpp │ ├── QGLSLCompleter.hpp │ ├── QGLSLHighlighter.hpp │ ├── QHighlightBlockRule.hpp │ ├── QHighlightRule.hpp │ ├── QJSHighlighter.hpp │ ├── QJSONHighlighter.hpp │ ├── QJavaHighlighter.hpp │ ├── QLanguage.hpp │ ├── QLineNumberArea.hpp │ ├── QLuaCompleter.hpp │ ├── QLuaHighlighter.hpp │ ├── QPythonCompleter.hpp │ ├── QPythonHighlighter.hpp │ ├── QStyleSyntaxHighlighter.hpp │ ├── QSyntaxStyle.hpp │ └── QXMLHighlighter.hpp ├── resources ├── default_style.xml ├── languages │ ├── cpp.xml │ ├── glsl.xml │ ├── java.xml │ ├── js.xml │ ├── lua.xml │ └── python.xml └── qcodeeditor_resources.qrc └── src └── internal ├── QCXXHighlighter.cpp ├── QCodeEditor.cpp ├── QGLSLCompleter.cpp ├── QGLSLHighlighter.cpp ├── QJSHighlighter.cpp ├── QJSONHighlighter.cpp ├── QJavaHighlighter.cpp ├── QLanguage.cpp ├── QLineNumberArea.cpp ├── QLuaCompleter.cpp ├── QLuaHighlighter.cpp ├── QPythonCompleter.cpp ├── QPythonHighlighter.cpp ├── QStyleSyntaxHighlighter.cpp ├── QSyntaxStyle.cpp └── QXMLHighlighter.cpp /.clang-format: -------------------------------------------------------------------------------- 1 | --- 2 | Language: Cpp 3 | # BasedOnStyle: Microsoft 4 | AccessModifierOffset: -2 5 | AlignAfterOpenBracket: Align 6 | AlignConsecutiveMacros: false 7 | AlignConsecutiveAssignments: false 8 | AlignConsecutiveDeclarations: false 9 | AlignEscapedNewlines: Right 10 | AlignOperands: true 11 | AlignTrailingComments: true 12 | AllowAllArgumentsOnNextLine: true 13 | AllowAllConstructorInitializersOnNextLine: true 14 | AllowAllParametersOfDeclarationOnNextLine: true 15 | AllowShortBlocksOnASingleLine: false 16 | AllowShortCaseLabelsOnASingleLine: false 17 | AllowShortFunctionsOnASingleLine: None 18 | AllowShortLambdasOnASingleLine: All 19 | AllowShortIfStatementsOnASingleLine: Never 20 | AllowShortLoopsOnASingleLine: false 21 | AlwaysBreakAfterDefinitionReturnType: None 22 | AlwaysBreakAfterReturnType: None 23 | AlwaysBreakBeforeMultilineStrings: false 24 | AlwaysBreakTemplateDeclarations: MultiLine 25 | BinPackArguments: true 26 | BinPackParameters: true 27 | BraceWrapping: 28 | AfterCaseLabel: false 29 | AfterClass: true 30 | AfterControlStatement: true 31 | AfterEnum: true 32 | AfterFunction: true 33 | AfterNamespace: true 34 | AfterObjCDeclaration: true 35 | AfterStruct: true 36 | AfterUnion: false 37 | AfterExternBlock: true 38 | BeforeCatch: true 39 | BeforeElse: true 40 | IndentBraces: false 41 | SplitEmptyFunction: true 42 | SplitEmptyRecord: true 43 | SplitEmptyNamespace: true 44 | BreakBeforeBinaryOperators: None 45 | BreakBeforeBraces: Custom 46 | BreakBeforeInheritanceComma: false 47 | BreakInheritanceList: BeforeColon 48 | BreakBeforeTernaryOperators: true 49 | BreakConstructorInitializersBeforeComma: false 50 | BreakConstructorInitializers: BeforeColon 51 | BreakAfterJavaFieldAnnotations: false 52 | BreakStringLiterals: true 53 | ColumnLimit: 120 54 | CommentPragmas: '^ IWYU pragma:' 55 | CompactNamespaces: false 56 | ConstructorInitializerAllOnOneLineOrOnePerLine: false 57 | ConstructorInitializerIndentWidth: 4 58 | ContinuationIndentWidth: 4 59 | Cpp11BracedListStyle: true 60 | DerivePointerAlignment: false 61 | DisableFormat: false 62 | ExperimentalAutoDetectBinPacking: false 63 | FixNamespaceComments: true 64 | ForEachMacros: 65 | - foreach 66 | - Q_FOREACH 67 | - BOOST_FOREACH 68 | IncludeBlocks: Preserve 69 | IncludeCategories: 70 | - Regex: '^"(llvm|llvm-c|clang|clang-c)/' 71 | Priority: 2 72 | - Regex: '^(<|"(gtest|gmock|isl|json)/)' 73 | Priority: 3 74 | - Regex: '.*' 75 | Priority: 1 76 | IncludeIsMainRegex: '(Test)?$' 77 | IndentCaseLabels: false 78 | IndentPPDirectives: None 79 | IndentWidth: 4 80 | IndentWrappedFunctionNames: false 81 | JavaScriptQuotes: Leave 82 | JavaScriptWrapImports: true 83 | KeepEmptyLinesAtTheStartOfBlocks: true 84 | MacroBlockBegin: '' 85 | MacroBlockEnd: '' 86 | MaxEmptyLinesToKeep: 1 87 | NamespaceIndentation: None 88 | ObjCBinPackProtocolList: Auto 89 | ObjCBlockIndentWidth: 2 90 | ObjCSpaceAfterProperty: false 91 | ObjCSpaceBeforeProtocolList: true 92 | PenaltyBreakAssignment: 2 93 | PenaltyBreakBeforeFirstCallParameter: 19 94 | PenaltyBreakComment: 300 95 | PenaltyBreakFirstLessLess: 120 96 | PenaltyBreakString: 1000 97 | PenaltyBreakTemplateDeclaration: 10 98 | PenaltyExcessCharacter: 1000000 99 | PenaltyReturnTypeOnItsOwnLine: 1000 100 | PointerAlignment: Right 101 | ReflowComments: true 102 | SortIncludes: true 103 | SortUsingDeclarations: true 104 | SpaceAfterCStyleCast: false 105 | SpaceAfterLogicalNot: false 106 | SpaceAfterTemplateKeyword: true 107 | SpaceBeforeAssignmentOperators: true 108 | SpaceBeforeCpp11BracedList: false 109 | SpaceBeforeCtorInitializerColon: true 110 | SpaceBeforeInheritanceColon: true 111 | SpaceBeforeParens: ControlStatements 112 | SpaceBeforeRangeBasedForLoopColon: true 113 | SpaceInEmptyParentheses: false 114 | SpacesBeforeTrailingComments: 1 115 | SpacesInAngles: false 116 | SpacesInContainerLiterals: true 117 | SpacesInCStyleCastParentheses: false 118 | SpacesInParentheses: false 119 | SpacesInSquareBrackets: false 120 | Standard: Cpp11 121 | StatementMacros: 122 | - Q_UNUSED 123 | - QT_REQUIRE_VERSION 124 | TabWidth: 4 125 | UseTab: Never 126 | ... 127 | 128 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # EditorConfig file (https://editorconfig.org) for the QCodeEditor project 2 | 3 | root = true 4 | 5 | [*] 6 | end_of_line = lf 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | # QCodeEditor C++ files 11 | [**.cpp] 12 | indent_style = space 13 | indent_size = 4 14 | [**.hpp] 15 | indent_style = space 16 | indent_size = 2 17 | 18 | # QCodeEditor XML files 19 | [**.xml] 20 | indent_style = space 21 | indent_size = 4 22 | [**.qrc] 23 | indent_style = space 24 | indent_size = 4 25 | 26 | # QCodeEditor CMake files 27 | [CMakeLists.txt] 28 | indent_style = space 29 | indent_size = 4 30 | -------------------------------------------------------------------------------- /.github/workflows/build_test.yml: -------------------------------------------------------------------------------- 1 | name: "CI: Build Test" 2 | 3 | on: 4 | push: 5 | paths-ignore: 6 | - '*.md' 7 | - '*/*.md' 8 | - '*/*/*.md' 9 | pull_request: 10 | paths-ignore: 11 | - '*.md' 12 | - '*/*.md' 13 | - '*/*/*.md' 14 | 15 | env: 16 | CMAKE_VERSION: 3.17.1 17 | NINJA_VERSION: 1.10.0 18 | BUILD_TYPE: Release 19 | CCACHE_VERSION: 3.7.7 20 | 21 | jobs: 22 | build: 23 | name: "${{ matrix.config.os }} Qt ${{ matrix.qt_version }}" 24 | runs-on: ${{ matrix.config.os }} 25 | strategy: 26 | fail-fast: false 27 | matrix: 28 | qt_version: [5.12.8, 5.14.2] 29 | config: 30 | - { 31 | os: windows-latest, 32 | cc: "cl", cxx: "cl", 33 | binary: 'cpeditor.exe', 34 | environment_script: "C:/Program Files (x86)/Microsoft Visual Studio/2019/Enterprise/VC/Auxiliary/Build/vcvars64.bat" 35 | } 36 | - { 37 | os: ubuntu-latest, 38 | cc: "gcc", cxx: "g++", 39 | binary: 'cpeditor' 40 | } 41 | - { 42 | os: macos-latest, 43 | cc: "clang", cxx: "clang++", 44 | binary: 'cpeditor.app/Contents/MacOS/cpeditor' 45 | } 46 | 47 | steps: 48 | - uses: actions/checkout@v2 49 | 50 | - name: Install Python 3.7 51 | uses: actions/setup-python@v1 52 | with: 53 | python-version: 3.7 54 | 55 | - name: Restore Qt from cache 56 | id: cache-qt 57 | uses: actions/cache@v1 58 | with: 59 | path: ../Qt 60 | key: Qt-${{ matrix.config.os }}-${{ matrix.qt_version }} 61 | 62 | - name: Set up Qt environment 63 | uses: jurplel/install-qt-action@v2 64 | with: 65 | cached: ${{ steps.cache-qt.outputs.cache-hit }} 66 | version: ${{ matrix.qt_version }} 67 | 68 | - name: Download Ninja and CMake 69 | id: cmake_and_ninja 70 | shell: cmake -P {0} 71 | run: | 72 | set(cmake_version $ENV{CMAKE_VERSION}) 73 | set(ninja_version $ENV{NINJA_VERSION}) 74 | message(STATUS "Using host CMake version: ${CMAKE_VERSION}") 75 | if ("${{ runner.os }}" STREQUAL "Windows") 76 | set(ninja_suffix "win.zip") 77 | set(cmake_suffix "win64-x64.zip") 78 | set(cmake_dir "cmake-${cmake_version}-win64-x64/bin") 79 | elseif ("${{ runner.os }}" STREQUAL "Linux") 80 | set(ninja_suffix "linux.zip") 81 | set(cmake_suffix "Linux-x86_64.tar.gz") 82 | set(cmake_dir "cmake-${cmake_version}-Linux-x86_64/bin") 83 | elseif ("${{ runner.os }}" STREQUAL "macOS") 84 | set(ninja_suffix "mac.zip") 85 | set(cmake_suffix "Darwin-x86_64.tar.gz") 86 | set(cmake_dir "cmake-${cmake_version}-Darwin-x86_64/CMake.app/Contents/bin") 87 | endif() 88 | set(ninja_url "https://github.com/ninja-build/ninja/releases/download/v${ninja_version}/ninja-${ninja_suffix}") 89 | file(DOWNLOAD "${ninja_url}" ./ninja.zip SHOW_PROGRESS) 90 | execute_process(COMMAND ${CMAKE_COMMAND} -E tar xvf ./ninja.zip) 91 | set(cmake_url "https://github.com/Kitware/CMake/releases/download/v${cmake_version}/cmake-${cmake_version}-${cmake_suffix}") 92 | file(DOWNLOAD "${cmake_url}" ./cmake.zip SHOW_PROGRESS) 93 | execute_process(COMMAND ${CMAKE_COMMAND} -E tar xvf ./cmake.zip) 94 | # Save the path for other steps 95 | file(TO_CMAKE_PATH "$ENV{GITHUB_WORKSPACE}/${cmake_dir}" cmake_dir) 96 | message("::set-output name=cmake_dir::${cmake_dir}") 97 | if (NOT "${{ runner.os }}" STREQUAL "Windows") 98 | execute_process( 99 | COMMAND chmod +x ninja 100 | COMMAND chmod +x ${cmake_dir}/cmake 101 | ) 102 | endif() 103 | 104 | - name: Download ccache 105 | id: ccache 106 | shell: cmake -P {0} 107 | run: | 108 | set(ccache_url "https://github.com/cristianadam/ccache/releases/download/v$ENV{CCACHE_VERSION}/${{ runner.os }}.tar.xz") 109 | file(DOWNLOAD "${ccache_url}" ./ccache.tar.xz SHOW_PROGRESS) 110 | execute_process(COMMAND ${CMAKE_COMMAND} -E tar xvf ./ccache.tar.xz) 111 | 112 | - name: Prepare ccache timestamp 113 | id: ccache_cache_timestamp 114 | shell: cmake -P {0} 115 | run: | 116 | string(TIMESTAMP current_date "%Y-%m-%d-%H;%M;%S" UTC) 117 | message("::set-output name=timestamp::${current_date}") 118 | 119 | - name: Restore ccache cache files 120 | uses: actions/cache@v1 121 | with: 122 | path: .ccache 123 | key: ${{ matrix.config.os }}-${{ matrix.qt_version }}-ccache-${{ steps.ccache_cache_timestamp.outputs.timestamp }} 124 | restore-keys: | 125 | ${{ matrix.config.os }}-${{ matrix.qt_version }}-ccache- 126 | 127 | - name: Configure CMake 128 | shell: cmake -P {0} 129 | run: | 130 | set(ENV{CC} ${{ matrix.config.cc }}) 131 | set(ENV{CXX} ${{ matrix.config.cxx }}) 132 | if ("${{ runner.os }}" STREQUAL "Windows" AND NOT "x${{ matrix.config.environment_script }}" STREQUAL "x") 133 | execute_process( 134 | COMMAND "${{ matrix.config.environment_script }}" && set 135 | OUTPUT_FILE environment_script_output.txt 136 | ) 137 | file(STRINGS environment_script_output.txt output_lines) 138 | foreach(line IN LISTS output_lines) 139 | if (line MATCHES "^([a-zA-Z0-9_-]+)=(.*)$") 140 | set(ENV{${CMAKE_MATCH_1}} "${CMAKE_MATCH_2}") 141 | endif() 142 | endforeach() 143 | endif() 144 | set(path_separator ":") 145 | if ("${{ runner.os }}" STREQUAL "Windows") 146 | set(path_separator ";") 147 | endif() 148 | set(ENV{PATH} "$ENV{GITHUB_WORKSPACE}${path_separator}$ENV{PATH}") 149 | execute_process( 150 | COMMAND ${{ steps.cmake_and_ninja.outputs.cmake_dir }}/cmake 151 | -S . 152 | -B build 153 | -D BUILD_EXAMPLE=On 154 | -D CMAKE_BUILD_TYPE=$ENV{BUILD_TYPE} 155 | -G Ninja 156 | -D CMAKE_MAKE_PROGRAM=ninja 157 | -D CMAKE_C_COMPILER_LAUNCHER=ccache 158 | -D CMAKE_CXX_COMPILER_LAUNCHER=ccache 159 | RESULT_VARIABLE result 160 | ) 161 | if (NOT result EQUAL 0) 162 | message(FATAL_ERROR "Bad exit status") 163 | endif() 164 | 165 | - name: Build 166 | shell: cmake -P {0} 167 | run: | 168 | set(ENV{NINJA_STATUS} "[%f/%t %o/sec] ") 169 | if ("${{ runner.os }}" STREQUAL "Windows" AND NOT "x${{ matrix.config.environment_script }}" STREQUAL "x") 170 | file(STRINGS environment_script_output.txt output_lines) 171 | foreach(line IN LISTS output_lines) 172 | if (line MATCHES "^([a-zA-Z0-9_-]+)=(.*)$") 173 | set(ENV{${CMAKE_MATCH_1}} "${CMAKE_MATCH_2}") 174 | endif() 175 | endforeach() 176 | endif() 177 | set(path_separator ":") 178 | if ("${{ runner.os }}" STREQUAL "Windows") 179 | set(path_separator ";") 180 | endif() 181 | set(ENV{PATH} "$ENV{GITHUB_WORKSPACE}${path_separator}$ENV{PATH}") 182 | file(TO_CMAKE_PATH "$ENV{GITHUB_WORKSPACE}" ccache_basedir) 183 | set(ENV{CCACHE_BASEDIR} "${ccache_basedir}") 184 | set(ENV{CCACHE_DIR} "${ccache_basedir}/.ccache") 185 | set(ENV{CCACHE_COMPRESS} "true") 186 | set(ENV{CCACHE_COMPRESSLEVEL} "6") 187 | set(ENV{CCACHE_MAXSIZE} "100M") 188 | if ("${{ matrix.config.cxx }}" STREQUAL "cl") 189 | set(ENV{CCACHE_MAXSIZE} "150M") 190 | endif() 191 | execute_process(COMMAND ccache -p) 192 | execute_process(COMMAND ccache -z) 193 | execute_process( 194 | COMMAND ${{ steps.cmake_and_ninja.outputs.cmake_dir }}/cmake --build build 195 | RESULT_VARIABLE result 196 | ) 197 | if (NOT result EQUAL 0) 198 | message(FATAL_ERROR "Bad exit status") 199 | endif() 200 | execute_process(COMMAND ccache -s) 201 | -------------------------------------------------------------------------------- /.github/workflows/check_format.yml: -------------------------------------------------------------------------------- 1 | name: "Check Format" 2 | 3 | on: [push, pull_request] 4 | 5 | jobs: 6 | format: 7 | name: "Check Clang Format" 8 | runs-on: ubuntu-latest 9 | steps: 10 | - uses: actions/checkout@v2 11 | - name: "Install clang-format-9" 12 | run: | 13 | sudo apt-get update 14 | sudo apt-get install clang-format-9 15 | - name: "Format Codes" 16 | run: clang-format-9 -i src/internal/*.cpp include/internal/*.hpp 17 | - name: Check diff 18 | run: git diff --exit-code HEAD 19 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | build/ 2 | cmake-build-debug/ 3 | cmake-build-release/ 4 | .idea/ 5 | .kdev4/ 6 | QCodeEditor.kdev4 7 | *~ 8 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.6) 2 | project(QCodeEditor) 3 | 4 | set(CMAKE_CXX_STANDARD 17) 5 | 6 | option(BUILD_EXAMPLE "Example building required" Off) 7 | 8 | if (${BUILD_EXAMPLE}) 9 | message(STATUS "QCodeEditor example will be built.") 10 | add_subdirectory(example) 11 | endif() 12 | 13 | set(RESOURCES_FILE 14 | resources/qcodeeditor_resources.qrc 15 | ) 16 | 17 | set(INCLUDE_FILES 18 | include/QHighlightRule 19 | include/QHighlightBlockRule 20 | include/QCodeEditor 21 | include/QCXXHighlighter 22 | include/QLineNumberArea 23 | include/QStyleSyntaxHighlighter 24 | include/QSyntaxStyle 25 | include/QGLSLCompleter 26 | include/QGLSLHighlighter 27 | include/QJavaHighlighter 28 | include/QJSHighlighter 29 | include/QLanguage 30 | include/QXMLHighlighter 31 | include/QJSONHighlighter 32 | include/QLuaCompleter 33 | include/QLuaHighlighter 34 | include/QPythonHighlighter 35 | include/internal/QHighlightRule.hpp 36 | include/internal/QHighlightBlockRule.hpp 37 | include/internal/QCodeEditor.hpp 38 | include/internal/QCXXHighlighter.hpp 39 | include/internal/QJavaHighlighter.hpp 40 | include/internal/QJSHighlighter.hpp 41 | include/internal/QLineNumberArea.hpp 42 | include/internal/QStyleSyntaxHighlighter.hpp 43 | include/internal/QSyntaxStyle.hpp 44 | include/internal/QGLSLCompleter.hpp 45 | include/internal/QGLSLHighlighter.hpp 46 | include/internal/QLanguage.hpp 47 | include/internal/QXMLHighlighter.hpp 48 | include/internal/QJSONHighlighter.hpp 49 | include/internal/QLuaCompleter.hpp 50 | include/internal/QLuaHighlighter.hpp 51 | include/internal/QPythonCompleter.hpp 52 | include/internal/QPythonHighlighter.hpp 53 | ) 54 | 55 | set(SOURCE_FILES 56 | src/internal/QCodeEditor.cpp 57 | src/internal/QLineNumberArea.cpp 58 | src/internal/QCXXHighlighter.cpp 59 | src/internal/QSyntaxStyle.cpp 60 | src/internal/QStyleSyntaxHighlighter.cpp 61 | src/internal/QGLSLCompleter.cpp 62 | src/internal/QGLSLHighlighter.cpp 63 | src/internal/QJavaHighlighter.cpp 64 | src/internal/QJSHighlighter.cpp 65 | src/internal/QLanguage.cpp 66 | src/internal/QXMLHighlighter.cpp 67 | src/internal/QJSONHighlighter.cpp 68 | src/internal/QLuaCompleter.cpp 69 | src/internal/QLuaHighlighter.cpp 70 | src/internal/QPythonCompleter.cpp 71 | src/internal/QPythonHighlighter.cpp 72 | ) 73 | 74 | # Create code for QObjects 75 | set(CMAKE_AUTOMOC On) 76 | 77 | # Create code from resource files 78 | set(CMAKE_AUTORCC ON) 79 | 80 | # Generate compile_commands.json in build/ for analyzers like clang-tidy. 81 | set(CMAKE_EXPORT_COMPILE_COMMANDS ON) 82 | 83 | # Find includes in corresponding build directories 84 | find_package(Qt5Core CONFIG REQUIRED) 85 | find_package(Qt5Widgets CONFIG REQUIRED) 86 | find_package(Qt5Gui CONFIG REQUIRED) 87 | 88 | add_library(QCodeEditor STATIC 89 | ${RESOURCES_FILE} 90 | ${SOURCE_FILES} 91 | ${INCLUDE_FILES} 92 | ) 93 | 94 | target_include_directories(QCodeEditor PUBLIC 95 | include 96 | ) 97 | 98 | if(CMAKE_COMPILER_IS_GNUCXX) 99 | target_compile_options(QCodeEditor 100 | PRIVATE 101 | -pedantic 102 | -Wall 103 | -Wextra 104 | -Weffc++ 105 | -Woverloaded-virtual 106 | -Winit-self 107 | -Wunreachable-code 108 | ) 109 | endif(CMAKE_COMPILER_IS_GNUCXX) 110 | 111 | target_link_libraries(QCodeEditor 112 | Qt5::Core 113 | Qt5::Widgets 114 | Qt5::Gui 115 | ) 116 | -------------------------------------------------------------------------------- /LICENSE.MIT: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2013-2019 Megaxela 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Qt Code Editor Widget 2 | 3 | [![GitHub Workflow Status](https://github.com/cpeditor/QCodeEditor/workflows/CI:%20Build%20Test/badge.svg?event=push)](https://github.com/cpeditor/QCodeEditor/actions?query=event%3Apush) 4 | 5 | It's a widget for editing/viewing code. 6 | 7 | This project uses a resource named `qcodeeditor_resources.qrc`. The main application 8 | must not use a resource file with the same name. 9 | 10 | (It's not a project from a Qt example.) 11 | 12 | ## Requirements 13 | 0. C++11 featured compiler. 14 | 0. Qt 5. 15 | 16 | ## Abilities 17 | 1. Auto parentheses. 18 | 1. Different highlight rules. 19 | 1. Auto indentation. 20 | 1. Replace tabs with spaces. 21 | 1. GLSL completion rules. 22 | 1. GLSL highlight rules. 23 | 1. C++ highlight rules. 24 | 1. XML highlight rules. 25 | 1. JSON highligh rules. 26 | 1. Java highligh rules. 27 | 1. JavaScript highligh rules. 28 | 1. Frame selection. 29 | 1. Qt Creator styles. 30 | 31 | ## Build 32 | It's a CMake-based library, so it can be used as a submodule (see the example). 33 | But here are the steps to build it as a static library (for external use for example). 34 | 35 | 1. Clone the repository: `git clone https://github.com/Megaxela/QCodeEditor` 36 | 1. Go into the repository: `cd QCodeEditor` 37 | 1. Create a build folder: `mkdir build` 38 | 1. Go into the build folder: `cd build` 39 | 1. Generate a build file for your compiler: `cmake ..` 40 | 1. If you need to build the example, specify `-DBUILD_EXAMPLE=On` on this step. 41 | 1. Build the library: `cmake --build .` 42 | 43 | ## Example 44 | 45 | By default, `QCodeEditor` uses the standard QtCreator theme. But you may specify 46 | your own by parsing it with `QSyntaxStyle`. The example uses [Dracula](https://draculatheme.com) theme. 47 | (See the example for more.) 48 | 49 | 50 | 51 | ## LICENSE 52 | 53 | 54 | 55 | Library is licensed under the [MIT License](https://opensource.org/licenses/MIT) 56 | 57 | Permission is hereby granted, free of charge, to any person obtaining a copy 58 | of this software and associated documentation files (the "Software"), to deal 59 | in the Software without restriction, including without limitation the rights 60 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 61 | copies of the Software, and to permit persons to whom the Software is 62 | furnished to do so, subject to the following conditions: 63 | 64 | The above copyright notice and this permission notice shall be included in all 65 | copies or substantial portions of the Software. 66 | 67 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 68 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 69 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 70 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 71 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 72 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 73 | SOFTWARE. 74 | -------------------------------------------------------------------------------- /example/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.6) 2 | project(QCodeEditorExample) 3 | 4 | set(CMAKE_CXX_STANDARD 17) 5 | 6 | set(CMAKE_AUTOMOC On) 7 | set(CMAKE_AUTORCC ON) 8 | 9 | find_package(Qt5Core CONFIG REQUIRED) 10 | find_package(Qt5Widgets CONFIG REQUIRED) 11 | find_package(Qt5Gui CONFIG REQUIRED) 12 | 13 | add_executable(QCodeEditorExample 14 | resources/demo_resources.qrc 15 | src/main.cpp 16 | src/MainWindow.cpp 17 | include/MainWindow.hpp 18 | ) 19 | 20 | target_include_directories(QCodeEditorExample PUBLIC 21 | include 22 | ) 23 | 24 | target_link_libraries(QCodeEditorExample 25 | Qt5::Core 26 | Qt5::Widgets 27 | Qt5::Gui 28 | QCodeEditor 29 | ) -------------------------------------------------------------------------------- /example/image/preview.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpeditor/QCodeEditor/ed1196a91dd6415c5ad6d0e85a90630e9b3b9f6c/example/image/preview.png -------------------------------------------------------------------------------- /example/include/MainWindow.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | // Qt 4 | #include // Required for inheritance 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | class QVBoxLayout; 13 | class QSyntaxStyle; 14 | class QComboBox; 15 | class QCheckBox; 16 | class QSpinBox; 17 | class QCompleter; 18 | class QStyleSyntaxHighlighter; 19 | class QCodeEditor; 20 | 21 | /** 22 | * @brief Class, that describes demo main window. 23 | */ 24 | class MainWindow : public QMainWindow 25 | { 26 | Q_OBJECT 27 | 28 | public: 29 | 30 | /** 31 | * @brief Constructor. 32 | * @param parent Pointer to parent widget. 33 | */ 34 | explicit MainWindow(QWidget* parent=nullptr); 35 | 36 | private: 37 | 38 | void loadStyle(QString path); 39 | 40 | QString loadCode(QString path); 41 | 42 | void initData(); 43 | 44 | void createWidgets(); 45 | 46 | void setupWidgets(); 47 | 48 | void performConnections(); 49 | 50 | QVBoxLayout* m_setupLayout; 51 | 52 | QComboBox* m_codeSampleCombobox; 53 | QComboBox* m_highlighterCombobox; 54 | QComboBox* m_completerCombobox; 55 | QComboBox* m_styleCombobox; 56 | 57 | QCheckBox* m_readOnlyCheckBox; 58 | QCheckBox* m_wordWrapCheckBox; 59 | QCheckBox* m_tabReplaceEnabledCheckbox; 60 | QSpinBox* m_tabReplaceNumberSpinbox; 61 | QCheckBox* m_autoIndentationCheckbox; 62 | 63 | QMenu * m_mainMenu; 64 | QAction * m_actionToggleComment; 65 | QAction * m_actionToggleBlockComment; 66 | 67 | QCodeEditor* m_codeEditor; 68 | 69 | QVector> m_codeSamples; 70 | QVector> m_completers; 71 | QVector> m_highlighters; 72 | QVector> m_styles; 73 | }; 74 | 75 | -------------------------------------------------------------------------------- /example/resources/code_samples/cxx.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int n, sum = 0; 6 | 7 | std::cout << "Enter a positive integer: "; 8 | std::cin >> n; 9 | 10 | for (int i = 1; i <= n; ++i) 11 | { 12 | sum += i; 13 | } 14 | 15 | std::cout << "Sum = " << sum; 16 | return 0; 17 | } -------------------------------------------------------------------------------- /example/resources/code_samples/java.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | /* This program is simply for demonstrating 4 | * the highlighting of Java syntax. 5 | */ 6 | 7 | class Animal { } 8 | 9 | class Dog extends Animal { } 10 | 11 | public class Example { 12 | /** Prompts the user for a string and returns what they entered. 13 | * @param message The message to display to the user. 14 | * @return The string that the user entered. 15 | * */ 16 | static String prompt(String message) { 17 | 18 | Scanner scanner = new Scanner(System.in); 19 | 20 | System.out.printf("%s: ", message); 21 | 22 | return scanner.nextLine(); 23 | } 24 | public static void main(String[] args) { 25 | 26 | double pi = 3.1415; 27 | 28 | String name = prompt("Enter your name"); 29 | 30 | System.out.println("Nice to meet you, " + name + "!"); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /example/resources/code_samples/js.js: -------------------------------------------------------------------------------- 1 | // example of a JS code to test JS syntax highlighter 2 | // source: 3 | // https://stackoverflow.com/questions/61301013/why-adapter-function-changing-my-num-array 4 | // https://stackoverflow.com/questions/61301471/how-can-i-filter-undefined-from-an-object 5 | 6 | var num = [] 7 | 8 | // creates a copy of a passed list, pushes 1 to it and returns it 9 | function push(nums) { 10 | var orig = nums.map(x => x) 11 | var newNum = pushed() 12 | num = orig 13 | return newNum 14 | } 15 | 16 | /* 17 | This is a helper function 18 | */ 19 | function pushed() { 20 | num.push(1) 21 | return num 22 | } 23 | 24 | function filterFunnel() { 25 | let filterLists = $("#funnel-filters").children().filter(".filter"); 26 | let query = `SELECT * FROM funnel WHERE `; 27 | let count = 0; 28 | for(i = 0; i < Object.keys(filterLists).length; i++) { 29 | let dom = filterLists[i]; 30 | if(dom === undefined){ 31 | return query; 32 | } else { 33 | if(dom.value === "Select" || dom.value === "") { 34 | 35 | } else { 36 | if(count >= 1) { 37 | query += ` AND ${dom.id} = ${dom.value}`; 38 | }else{ 39 | query += `${dom.id} = ${dom.value} ` 40 | } 41 | count += 1; 42 | } 43 | } 44 | } 45 | } 46 | 47 | console.log("new", push(num)) 48 | console.log("old", num) -------------------------------------------------------------------------------- /example/resources/code_samples/json.json: -------------------------------------------------------------------------------- 1 | { 2 | "root": { 3 | "some array": [1, 2, 3], 4 | "some string": "Hello world", 5 | "some dbl": 2.121323, 6 | "some bool": true, 7 | "some null": null 8 | } 9 | } -------------------------------------------------------------------------------- /example/resources/code_samples/lua.lua: -------------------------------------------------------------------------------- 1 | -- Two dashes start a one-line comment. 2 | --[[ 3 | Adding two ['s and ]'s makes it a 4 | multi-line comment. 5 | --]] 6 | 7 | ---------------------------------------------------- 8 | -- 1. Variables and flow control. 9 | ---------------------------------------------------- 10 | 11 | num = 42 -- All numbers are doubles. 12 | -- Don't freak out, 64-bit doubles have 52 bits for 13 | -- storing exact int values; machine precision is 14 | -- not a problem for ints that need < 52 bits. 15 | 16 | s = 'walternate' -- Immutable strings like Python. 17 | t = "double-quotes are also fine" 18 | u = [[ Double brackets 19 | start and end 20 | multi-line strings.]] 21 | t = nil -- Undefines t; Lua has garbage collection. 22 | 23 | -- Blocks are denoted with keywords like do/end: 24 | while num < 50 do 25 | num = num + 1 -- No ++ or += type operators. 26 | end 27 | 28 | -- If clauses: 29 | if num > 40 then 30 | print('over 40') 31 | elseif s ~= 'walternate' then -- ~= is not equals. 32 | -- Equality check is == like Python; ok for strs. 33 | io.write('not over 40\n') -- Defaults to stdout. 34 | else 35 | -- Variables are global by default. 36 | thisIsGlobal = 5 -- Camel case is common. 37 | 38 | -- How to make a variable local: 39 | local line = io.read() -- Reads next stdin line. 40 | 41 | -- String concatenation uses the .. operator: 42 | print('Winter is coming, ' .. line) 43 | end 44 | 45 | -- Undefined variables return nil. 46 | -- This is not an error: 47 | foo = anUnknownVariable -- Now foo = nil. 48 | 49 | aBoolValue = false 50 | 51 | -- Only nil and false are falsy; 0 and '' are true! 52 | if not aBoolValue then print('twas false') end 53 | 54 | -- 'or' and 'and' are short-circuited. 55 | -- This is similar to the a?b:c operator in C/js: 56 | ans = aBoolValue and 'yes' or 'no' --> 'no' 57 | 58 | karlSum = 0 59 | for i = 1, 100 do -- The range includes both ends. 60 | karlSum = karlSum + i 61 | end 62 | 63 | -- Use "100, 1, -1" as the range to count down: 64 | fredSum = 0 65 | for j = 100, 1, -1 do fredSum = fredSum + j end 66 | 67 | -- In general, the range is begin, end[, step]. 68 | 69 | -- Another loop construct: 70 | repeat 71 | print('the way of the future') 72 | num = num - 1 73 | until num == 0 74 | 75 | 76 | ---------------------------------------------------- 77 | -- 2. Functions. 78 | ---------------------------------------------------- 79 | 80 | function fib(n) 81 | if n < 2 then return 1 end 82 | return fib(n - 2) + fib(n - 1) 83 | end 84 | 85 | -- Closures and anonymous functions are ok: 86 | function adder(x) 87 | -- The returned function is created when adder is 88 | -- called, and remembers the value of x: 89 | return function (y) return x + y end 90 | end 91 | a1 = adder(9) 92 | a2 = adder(36) 93 | print(a1(16)) --> 25 94 | print(a2(64)) --> 100 95 | 96 | -- Returns, func calls, and assignments all work 97 | -- with lists that may be mismatched in length. 98 | -- Unmatched receivers are nil; 99 | -- unmatched senders are discarded. 100 | 101 | x, y, z = 1, 2, 3, 4 102 | -- Now x = 1, y = 2, z = 3, and 4 is thrown away. 103 | 104 | function bar(a, b, c) 105 | print(a, b, c) 106 | return 4, 8, 15, 16, 23, 42 107 | end 108 | 109 | x, y = bar('zaphod') --> print zaphod nil nil 110 | -- Now x = 4, y = 8, values 15..42 are discarded. 111 | 112 | -- Functions are first-class, may be local/global. 113 | -- These are the same: 114 | function f(x) return x * x end 115 | f = function (x) return x * x end 116 | 117 | -- And so are these: 118 | local function g(x) return math.sin(x) end 119 | local g; g = function (x) return math.sin(x) end 120 | -- the 'local g' decl makes g-self-references ok. 121 | 122 | -- Trig funcs work in radians, by the way. 123 | 124 | -- Calls with one string param don't need parens: 125 | print 'hello' -- Works fine. 126 | 127 | 128 | ---------------------------------------------------- 129 | -- 3. Tables. 130 | ---------------------------------------------------- 131 | 132 | -- Tables = Lua's only compound data structure; 133 | -- they are associative arrays. 134 | -- Similar to php arrays or js objects, they are 135 | -- hash-lookup dicts that can also be used as lists. 136 | 137 | -- Using tables as dictionaries / maps: 138 | 139 | -- Dict literals have string keys by default: 140 | t = {key1 = 'value1', key2 = false} 141 | 142 | -- String keys can use js-like dot notation: 143 | print(t.key1) -- Prints 'value1'. 144 | t.newKey = {} -- Adds a new key/value pair. 145 | t.key2 = nil -- Removes key2 from the table. 146 | 147 | -- Literal notation for any (non-nil) value as key: 148 | u = {['@!#'] = 'qbert', [{}] = 1729, [6.28] = 'tau'} 149 | print(u[6.28]) -- prints tau 150 | 151 | -- Key matching is basically by value for numbers 152 | -- and strings, but by identity for tables. 153 | a = u['@!#'] -- Now a = 'qbert'. 154 | b = u[{}] -- We might expect 1729, but it's nil: 155 | -- b = nil since the lookup fails. It fails 156 | -- because the key we used is not the same object 157 | -- as the one used to store the original value. So 158 | -- strings & numbers are more portable keys. 159 | 160 | -- A one-table-param function call needs no parens: 161 | function h(x) print(x.key1) end 162 | h{key1 = 'Sonmi~451'} -- Prints 'Sonmi~451'. 163 | 164 | for key, val in pairs(u) do -- Table iteration. 165 | print(key, val) 166 | end 167 | 168 | -- _G is a special table of all globals. 169 | print(_G['_G'] == _G) -- Prints 'true'. 170 | 171 | -- Using tables as lists / arrays: 172 | 173 | -- List literals implicitly set up int keys: 174 | v = {'value1', 'value2', 1.21, 'gigawatts'} 175 | for i = 1, #v do -- #v is the size of v for lists. 176 | print(v[i]) -- Indices start at 1 !! SO CRAZY! 177 | end 178 | -- A 'list' is not a real type. v is just a table 179 | -- with consecutive integer keys, treated as a list. 180 | 181 | ---------------------------------------------------- 182 | -- 3.1 Metatables and metamethods. 183 | ---------------------------------------------------- 184 | 185 | -- A table can have a metatable that gives the table 186 | -- operator-overloadish behavior. Later we'll see 187 | -- how metatables support js-prototypey behavior. 188 | 189 | f1 = {a = 1, b = 2} -- Represents the fraction a/b. 190 | f2 = {a = 2, b = 3} 191 | 192 | -- This would fail: 193 | -- s = f1 + f2 194 | 195 | metafraction = {} 196 | function metafraction.__add(f1, f2) 197 | sum = {} 198 | sum.b = f1.b * f2.b 199 | sum.a = f1.a * f2.b + f2.a * f1.b 200 | return sum 201 | end 202 | 203 | setmetatable(f1, metafraction) 204 | setmetatable(f2, metafraction) 205 | 206 | s = f1 + f2 -- call __add(f1, f2) on f1's metatable 207 | 208 | -- f1, f2 have no key for their metatable, unlike 209 | -- prototypes in js, so you must retrieve it as in 210 | -- getmetatable(f1). The metatable is a normal table 211 | -- with keys that Lua knows about, like __add. 212 | 213 | -- But the next line fails since s has no metatable: 214 | -- t = s + s 215 | -- Class-like patterns given below would fix this. 216 | 217 | -- An __index on a metatable overloads dot lookups: 218 | defaultFavs = {animal = 'gru', food = 'donuts'} 219 | myFavs = {food = 'pizza'} 220 | setmetatable(myFavs, {__index = defaultFavs}) 221 | eatenBy = myFavs.animal -- works! thanks, metatable 222 | 223 | -- Direct table lookups that fail will retry using 224 | -- the metatable's __index value, and this recurses. 225 | 226 | -- An __index value can also be a function(tbl, key) 227 | -- for more customized lookups. 228 | 229 | -- Values of __index,add, .. are called metamethods. 230 | -- Full list. Here a is a table with the metamethod. 231 | 232 | -- __add(a, b) for a + b 233 | -- __sub(a, b) for a - b 234 | -- __mul(a, b) for a * b 235 | -- __div(a, b) for a / b 236 | -- __mod(a, b) for a % b 237 | -- __pow(a, b) for a ^ b 238 | -- __unm(a) for -a 239 | -- __concat(a, b) for a .. b 240 | -- __len(a) for #a 241 | -- __eq(a, b) for a == b 242 | -- __lt(a, b) for a < b 243 | -- __le(a, b) for a <= b 244 | -- __index(a, b) for a.b 245 | -- __newindex(a, b, c) for a.b = c 246 | -- __call(a, ...) for a(...) 247 | 248 | ---------------------------------------------------- 249 | -- 3.2 Class-like tables and inheritance. 250 | ---------------------------------------------------- 251 | 252 | -- Classes aren't built in; there are different ways 253 | -- to make them using tables and metatables. 254 | 255 | -- Explanation for this example is below it. 256 | 257 | Dog = {} -- 1. 258 | 259 | function Dog:new() -- 2. 260 | newObj = {sound = 'woof'} -- 3. 261 | self.__index = self -- 4. 262 | return setmetatable(newObj, self) -- 5. 263 | end 264 | 265 | function Dog:makeSound() -- 6. 266 | print('I say ' .. self.sound) 267 | end 268 | 269 | mrDog = Dog:new() -- 7. 270 | mrDog:makeSound() -- 'I say woof' -- 8. 271 | 272 | -- 1. Dog acts like a class; it's really a table. 273 | -- 2. function tablename:fn(...) is the same as 274 | -- function tablename.fn(self, ...) 275 | -- The : just adds a first arg called self. 276 | -- Read 7 & 8 below for how self gets its value. 277 | -- 3. newObj will be an instance of class Dog. 278 | -- 4. self = the class being instantiated. Often 279 | -- self = Dog, but inheritance can change it. 280 | -- newObj gets self's functions when we set both 281 | -- newObj's metatable and self's __index to self. 282 | -- 5. Reminder: setmetatable returns its first arg. 283 | -- 6. The : works as in 2, but this time we expect 284 | -- self to be an instance instead of a class. 285 | -- 7. Same as Dog.new(Dog), so self = Dog in new(). 286 | -- 8. Same as mrDog.makeSound(mrDog); self = mrDog. 287 | 288 | ---------------------------------------------------- 289 | 290 | -- Inheritance example: 291 | 292 | LoudDog = Dog:new() -- 1. 293 | 294 | function LoudDog:makeSound() 295 | s = self.sound .. ' ' -- 2. 296 | print(s .. s .. s) 297 | end 298 | 299 | seymour = LoudDog:new() -- 3. 300 | seymour:makeSound() -- 'woof woof woof' -- 4. 301 | 302 | -- 1. LoudDog gets Dog's methods and variables. 303 | -- 2. self has a 'sound' key from new(), see 3. 304 | -- 3. Same as LoudDog.new(LoudDog), and converted to 305 | -- Dog.new(LoudDog) as LoudDog has no 'new' key, 306 | -- but does have __index = Dog on its metatable. 307 | -- Result: seymour's metatable is LoudDog, and 308 | -- LoudDog.__index = LoudDog. So seymour.key will 309 | -- = seymour.key, LoudDog.key, Dog.key, whichever 310 | -- table is the first with the given key. 311 | -- 4. The 'makeSound' key is found in LoudDog; this 312 | -- is the same as LoudDog.makeSound(seymour). 313 | 314 | -- If needed, a subclass's new() is like the base's: 315 | function LoudDog:new() 316 | newObj = {} 317 | -- set up newObj 318 | self.__index = self 319 | return setmetatable(newObj, self) 320 | end 321 | 322 | ---------------------------------------------------- 323 | -- 4. Modules. 324 | ---------------------------------------------------- 325 | 326 | 327 | --[[ I'm commenting out this section so the rest of 328 | -- this script remains runnable. 329 | -- Suppose the file mod.lua looks like this: 330 | local M = {} 331 | 332 | local function sayMyName() 333 | print('Hrunkner') 334 | end 335 | 336 | function M.sayHello() 337 | print('Why hello there') 338 | sayMyName() 339 | end 340 | 341 | return M 342 | 343 | -- Another file can use mod.lua's functionality: 344 | local mod = require('mod') -- Run the file mod.lua. 345 | 346 | -- require is the standard way to include modules. 347 | -- require acts like: (if not cached; see below) 348 | local mod = (function () 349 | 350 | end)() 351 | -- It's like mod.lua is a function body, so that 352 | -- locals inside mod.lua are invisible outside it. 353 | 354 | -- This works because mod here = M in mod.lua: 355 | mod.sayHello() -- Says hello to Hrunkner. 356 | 357 | -- This is wrong; sayMyName only exists in mod.lua: 358 | mod.sayMyName() -- error 359 | 360 | -- require's return values are cached so a file is 361 | -- run at most once, even when require'd many times. 362 | 363 | -- Suppose mod2.lua contains print('Hi!'). 364 | local a = require('mod2') -- Prints Hi! 365 | local b = require('mod2') -- Doesn't print; a=b. 366 | 367 | -- dofile is like require without caching: 368 | dofile('mod2.lua') --> Hi! 369 | dofile('mod2.lua') --> Hi! (runs it again) 370 | 371 | -- loadfile loads a lua file but doesn't run it yet. 372 | f = loadfile('mod2.lua') -- Call f() to run it. 373 | 374 | -- loadstring is loadfile for strings. 375 | g = loadstring('print(343)') -- Returns a function. 376 | g() -- Prints out 343; nothing printed before now. 377 | 378 | --]] 379 | 380 | ---------------------------------------------------- 381 | -- 5. References. 382 | ---------------------------------------------------- 383 | 384 | --[[ 385 | 386 | I was excited to learn Lua so I could make games 387 | with the Löve 2D game engine. That's the why. 388 | 389 | I started with BlackBulletIV's Lua for programmers. 390 | Next I read the official Programming in Lua book. 391 | That's the how. 392 | 393 | It might be helpful to check out the Lua short 394 | reference on lua-users.org. 395 | 396 | The main topics not covered are standard libraries: 397 | * string library 398 | * table library 399 | * math library 400 | * io library 401 | * os library 402 | 403 | By the way, this entire file is valid Lua; save it 404 | as learn.lua and run it with "lua learn.lua" ! 405 | 406 | This was first written for tylerneylon.com. It's 407 | also available as a github gist. Tutorials for other 408 | languages, in the same style as this one, are here: 409 | 410 | http://learnxinyminutes.com/ 411 | 412 | Have fun with Lua! 413 | 414 | --]] -------------------------------------------------------------------------------- /example/resources/code_samples/python.py: -------------------------------------------------------------------------------- 1 | import numpy 2 | 3 | def sqr_array(x): 4 | ''' 5 | Documentation for sqr_array. 6 | A multi line string!' 7 | ''' 8 | 9 | return np.array(x)**2 10 | 11 | s = 'this is a string' 12 | d = 10 13 | x = np.arange(0,d,d/10) 14 | y = sqr_array(x) 15 | -------------------------------------------------------------------------------- /example/resources/code_samples/shader.glsl: -------------------------------------------------------------------------------- 1 | precision mediump float; 2 | 3 | uniform vec2 resolution; 4 | uniform float time; 5 | 6 | vec3 trans(vec3 p) 7 | { 8 | return mod(p, 8.0)-4.0; 9 | } 10 | 11 | float distanceFunction(vec3 pos) 12 | { 13 | return length(trans(pos)) - 1.5; 14 | } 15 | 16 | vec3 getNormal(vec3 p) 17 | { 18 | const float d = 0.0001; 19 | return 20 | normalize 21 | ( 22 | vec3 23 | ( 24 | distanceFunction(p+vec3(d, 0.0, 0))-distanceFunction(p+vec3(-d,0.0,0.0)), 25 | distanceFunction(p+vec3(0.0, d, 0.0))-distanceFunction(p+vec3(0.0,-d,0.0)), 26 | distanceFunction(p+vec3(0.0, 0.0, d))-distanceFunction(p+vec3(0.0,0.0,-d)) 27 | ) 28 | ); 29 | } 30 | 31 | void main() { 32 | vec2 pos = (gl_FragCoord.xy*2.0 -resolution) / resolution.y; 33 | 34 | vec3 camPos = vec3(0.0, 0.0, 3.0); 35 | vec3 camDir = vec3(0.0, 0.0, -1.0); 36 | vec3 camUp = vec3(0.0, 1.0, 0.0); 37 | vec3 camSide = cross(camDir, camUp); 38 | float focus = sin(time)*1.5+4.0; 39 | 40 | mat3 lense = mat3(1.,0.,0., 41 | 0.,888989898989898989,0., 42 | 0.,0.,1.); 43 | vec3 pos3 = vec3(pos,camDir.z*10.); 44 | camDir = vec3( 45 | camDir.x, 46 | camDir.y, 47 | camDir.z); 48 | camDir*=normalize(dot(camDir,pos3)); 49 | vec3 rayDir = normalize(camSide*pos.x + camUp*pos.y + camDir*focus); 50 | 51 | float t = 0.0, d; 52 | vec3 posOnRay = camPos; 53 | 54 | for(int i=0; i<64; ++i) 55 | { 56 | d = distanceFunction(posOnRay); 57 | t += d; 58 | posOnRay = camPos + t*rayDir; 59 | } 60 | 61 | vec3 normal = getNormal(posOnRay); 62 | if(abs(d) < 0.001) 63 | { 64 | gl_FragColor = vec4(normal, 1.0); 65 | }else 66 | { 67 | gl_FragColor = vec4(0.0); 68 | } 69 | } -------------------------------------------------------------------------------- /example/resources/code_samples/xml.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Joe 7 | Bob 8 | Trenton Literary Review Honorable Mention 9 | 10 | 12 11 | 12 | 13 | 14 | Mary 15 | Bob 16 | Selected Short Stories of 17 | Mary 18 | Bob 19 | 20 | 21 | 22 | Britney 23 | Bob 24 | 25 | 55 26 | 27 | 28 | 2.50 29 | 30 | 31 | 32 | 33 | Toni 34 | Bob 35 | B.A. 36 | Ph.D. 37 | Pulitzer 38 | Still in Trenton 39 | Trenton Forever 40 | 41 | 6.50 42 | 43 |

It was a dark and stormy night.

44 |

But then all nights in Trenton seem dark and 45 | stormy to someone who has gone through what 46 | I have.

47 | 48 | Trenton 49 | misery 50 | 51 |
52 |
53 | 54 | Who's Who in Trenton 55 | Robert Bob 56 | 57 |
-------------------------------------------------------------------------------- /example/resources/demo_resources.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | styles/drakula.xml 4 | code_samples/cxx.cpp 5 | code_samples/shader.glsl 6 | code_samples/xml.xml 7 | code_samples/java.java 8 | code_samples/js.js 9 | code_samples/json.json 10 | code_samples/lua.lua 11 | code_samples/python.py 12 | 13 | 14 | -------------------------------------------------------------------------------- /example/resources/styles/drakula.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 |