kdevelop"> TeX"> LaTeX"> ]]> ]> O Manual de Programação do KDevelop O Guia do Utilizador para Desenvolvimento de Aplicações C++ para o Ambiente de Trabalho K (KDE) com o ambiente de trabalho KDevelop, Versão 2.1 Ralf Nolden A Equipa do KDevelop
<Ralf.Nolden@post.rwth-aachen.de>
Versão 2.1 , 7 de Julho, 1999 Este manual em si é parte do Ambiente de Desenvolvimento Integrado (IDE) KDevelop e é assim também licenciado sob a Licença Geral Pública GNU; veja Copyright para mais informação. KDE kdevelop
Introdução Como os Sistemas Unix estão a tornar-se mais e mais populares mesmo para iniciados no trabalho com computadores, devido às suas vantagens no que toca a estabilidade e functionalidade, muitos estão de certo modo desapontados, porque as aplicações não têm um aspecto consistente e cada uma se comporta de forma diferente das restantes. Com o KDE, os programadores têm uma forma quase perfeita de criar aplicações de primeira classe para ambientes de trabalho Unix para obterem uma comunidade de utilizadores mais vasta apenas pela qualidade que as suas aplicações oferecem. Assim, o KDE torna-se mais e mais popular como uma base para o desenvolvimento de aplicações, e os programadores desejam retirar vantagens das possibilidades que o sistema tem para oferecer. O que já deve saber neste momento Para obter a melhor utilização deste manual de programação, nós assumimos que você já conhece a linguagem de programação C++; se não, deve familiarizar-se com ela primeiro. Informação sobre C++ está disponível através de várias fontes quer em formato impresso na sua livraria local ou através de tutores encontrados na Internet. Conhecimentos sobre o desenho de Interfaces Gráficos de Utilizador não são requeridos, já que este manual tenta cobrir o desenho de aplicações para programas KDE, o que também inclui uma introdução ao conjunto de ferramentas Qt Qt bem como às bibliotecas KDE bibliotecas KDE e ao desenho de Interfaces de Utilizador. Também, já se deve ter familiarizado com o KDevelop tendo lido O Manual do Utilizador do KDevelop, que contém uma revisão descritiva das funcionalidades disponibilizadas pelo ambiente de desenvolvimento (IDE). Sobre este Manual Este manual foi escrito para dar aos programadores uma introdução ao desenvolvimento de aplicações para o KDE utilizando o Ambiente de Desenvolvimento Integrado KDevelop. Os capítulos seguintes dão-lhe assim uma introdução sobre como criar projectos, explica o código fonte já gerado e mostra como ampliar o código fornecido em vários aspectos tais como as barras de ferramentas ferramentas, barras de menu e áreas de vista. Depois é discutido em detalhe o editor de diálogos, explicando como os widgets são criados e cobre a definição de propriedades dos widget's em detalhe para todos os widgets disponibilizados. Finalmente, você irá aprender sobre vários tópicos que irão completar o seu conhecimento no que respeita ao desenho de projectos e o ajuda a resolver problemas adicionais para além de programar tais como adicionar documentação API API e extender os manuais electrónicos. No capítulo seguinte iremos observar as bibliotecas Qt Qt e KDE KDE, mostrando conceitos basicos e porque são as coisas do modo que são. Também, iremos discutir como criar as aplicações de tutor disponibilizadas com o conjunto de ferramentas Qt Qt utilizando o KDevelop, para que os iniciados possam ver já os primeiros resultados com alguns passos, e assim aprender como utilizar algumas das melhores funcionalidades do KDevelop. Nos capítulos seguintes você irá aprender: como criar uma aplicação com o KAppWizard, o que o esqueleto do projecto já disponibiliza, o que o código já criado significa, como criar as suas próprias vistas, como aumentar as funcionalidades da sua aplicação através de diálogos e barras de menu e ferramentas ferramentas como tornar a sua aplicação amiga do utilizador disponibilizando funções de ajuda e como escrever documentação electrónica em SGML SGML. Informação Adicional Informação adicional sobre programação Qt Qt/KDE é disponibilizada por várias fontes: Programar com a Qt Qt por Matthias Kalle Dalheimer, publicado por O'Reilly (veja \|\|, cobrindo quase todos os aspectos do conjunto de ferramentas GUI Qt Qt e contendo também exemplos. O Manual do Utilizador do KDevelop, disponibilizado com o IDE KDevelop, Referência Electrónica para a biblioteca Qt Qt, disponibilizada com a sua cópia do conjunto de ferramentas Qt Qt em HTML e disponível em PostScript em \|\| Na Internet, veja a página da Troll Tech em \|\|, a página do KDE em \|\|, a página do programador KDE em \|\| a página do KDevelop em \|\| Adicionalmente, deverá procurar ajuda subscrevendo-se nas várias listas de discussão por e-mail, cujos endereços estão disponíveis nas páginas mencionadas, e nos grupos de notícias da Usenet dedicados aos utilizadores do KDE e Sistemas Unix bem como sobre a linguagem de programação C e C++. Para obter ajuda sobre o IDE KDevelop, deverá enviar pedidos para a nossa lista de discussão em kdevelop@fara3.cs.uni-potsdam.de. Lembre-se que a equipa do KDevelop é dedicada a oferecer-lhe os meios para lhe permitir programar aplicações e assim não tem como objectivo ser uma equipa de suporte técnico em casos em que as aplicações que está a desenvolver não funcionam devido a erros de implementação ou más configurações do seu sistema operativo. Posto isto, nós pedimos a todos os utilizadores para aproveitarem a lista de discussão em todos os casos em que encontrarem problemas com a utilização do IDE em si, bem como para reportar erros (bugs) e sugestões para melhorar a funcionalidade do ambiente de desenvolvimento. As Bibliotecas KDE e Qt <indexterm remap="idx"><primary>Qt</primary></indexterm> A empresa Norueguesa Troll Tech (\|\|) disponibiliza um chamado conjunto de ferramentas de GUI, chamado Qt Qt. Assim, GUI significa "Graphical User Interface - Interface Gráfico de Utilizador", e assim, as aplicações baseadas na Qt Qt representam-se com botões, janelas etc, permitindo inserção de dados do utilizador visualizando as funções que a aplicação disponibiliza. Este conjunto de ferramentas é necessário para o desenvolvimento de aplicações gráficas que corram no Interface X-Window em Sistemas Unix, porque o X não contém um interface de utilizador pré-definido em si. Apesar de outros conjuntos de ferramentas estarem também disponíveis para criar Interfaces de Utilizador, a Qt Qt oferece algumas vantagens técnicas que tornam o desenho de aplicações muito fácil. Adicionalmente, o conjunto de ferramentas Qt Qt também está disponível para o sistema Microsoft Windows, o que permite aos programadores disponibilizarem as suas aplicações para ambas as plataformas. A Equipa KDE (\|\|) juntou-se com o objectivo de tornar a utilização de Sistemas Unix mais amigável, e decidiu utilizar o conjunto de ferramentas Qt Qt para o desenvolvimento de um gestor de janelas no X-Window, mais uma variedade de ferramentas incluídas com os pacotes KDE. O Ambiente de Trabalho K contém assim o gestor de janelas kwm, o gestor de ficheiros kfm e o painel de controlo kpanel como sendo os componentes principais mais uma variedade de utilitários e aplicações de primeira classe. Depois do KDE ter saido, muitos dos programadores voltaram os seus olhos no sentido deste novo ambiente e do que tem para lhes oferecer. As bibliotecas KDE bibliotecas KDE estão a disponibilizar métodos e classes fundamentais que fazem com que todas as aplicações desenhadas com elas tenham um aspecto similar e consistente, e o utilizador tem a grande vantagem de apenas ter de se acostumar com a utilização específica de uma aplicação, não com o manuseamento de diálogos ou botões. Também, os programas KDE integram-se no ambiente de trabalho e são capazes de interagir com o gestor de ficheiros através do arrastar e largar arrastar e largar (drag and drop), oferecer gestão de sessões e muito mais, se todas as funcionalidades oferecidas pelas bibliotecas KDE bibliotecas KDE forem utilizadas. Ambos, o conjunto de ferramentas Qt Qt e as bibliotecas KDE bibliotecas KDE, são implementadas na linguagem de programação C++; assim aplicações que façam uso destas bibliotecas são também maioritariamente escritas em C++. No capítulo seguinte, faremos uma breve viagem através das bibliotecas para ver o que já é disponibilizado e como as aplicações Qt Qt e KDE KDE são criadas em geral. O Conjunto de Ferramentas GUI Qt <indexterm remap="idx"><primary>Qt</primary></indexterm> Como foi dito, a biblioteca Qt Qt é um conjunto de ferramentas que oferece elementos gráficos que são utilizados para criar GUI's de aplicações e são necessárias para programação X-Window. Adicionalmente, este conjunto de ferramentas oferece: Um conjunto completo de classes e métodos prontos a utilizar para questões de programação não-gráfica, Uma boa solução no sentido da interacção do utilizador através de métodos virtuais e mecanismos sinal/espaço, Um conjunto de elementos GUI pré-definidos, chamados "widgets", que podem ser facilmente utilizados para criar elementos visíveis Diálogos adicionais completamente pré-definidos que são muitas vezes utilizados em aplicaçãis tais como diálogos de progresso e de ficheiros. Posto isto conhecer as classes Qt Qt é essêncial, mesmo se você apenas deseja programar aplicações KDE. Para ter uma ideia do conceito basico sobre como as aplicações GUI são construidas e compiladas, iremos primeiro observar um programa exemplo apenas Qt Qt; depois extende-lo-emos para um programa KDE. A Primeira Aplicação Qt <indexterm remap="idx"><primary>Qt</primary></indexterm> Como normalmente, programas em C++ têm de conter a função main(), que é o ponto de partida para a execução da aplicação. Como queremos que seja graficamente visível em janelas e ofereça interacção do utilizador, temos primeiro que saber, como se podem mostrar a si próprias ao utilizador. Por exemplo, iremos observar o primeiro programa de tutor incluido com a Documentação Electrónica de Referência Qt Qt e explicar os passos de execução básicos; também como e porquê as janelas da aplicação aparecem: #include <qapplication.h> #include <qpushbutton.h> int main( int argc, char **argv ) { QApplication a( argc, argv ); QPushButton hello( "Hello world!" ); hello.resize( 100, 30 ); a.setMainWidget( &&;hello ); hello.show(); return a.exec(); } Esta aplicação meramente pinta uma janela contendo um botão com "Hello world" ("Olá Mundo") como texto. Como todas as aplicações baseadas na Qt Qt, primeiro tem de declarar uma instância da classe QApplicationQApplication, representada por a. Depois, o programa cria uma instância da classe QPushButton chamada hello, isto será o botão. O constructor do hello obtém uma string (conjunto de caracteres) como parâmetro, que é o conteudo do widget visível como sendo o texto do botão. Depois o método resize() é chamado sobre o botão hello. Isto muda o tamanho de defeito que um widget (que é neste caso o QPushButton) tem quando criado para o comprimento de 100 pixels e a altura de 30 pixels. Finalmente, o método setMainWidget() é chamado para a e o método show() para hello. A QApplicationQApplication é finalmente executada por a.exec(), entra no ciclo de acontecimento principal e aguarda até que tenha de devolver um valor inteiro para a camada do Sistema Operativo assinalando que a aplicação terminou. A Documentação de Referência para a Qt <indexterm remap="idx"><primary>Qt</primary></indexterm> Agora, vamos observar rapidamente a documentação de referência da biblioteca Qt Qt. Para fazer isto, inicie o KDevelop e seleccione "biblioteca Qt Qt" a partir do menu "Ajuda" na barra de menu. O navegador de documentação abre e mostra-lhe a página inicial da referência Qt Qt. Isto irá ser o seu primeiro local para obter informação sobre a Qt Qt, suas classes e as funções disponíveis que oferece. Também, o programa acima mostrado é o primeiro que é incluído na secção dos tutores. Para obter as classes que desejamos observar, QApplicationQApplication e QPushButton, seleccione "Lista Alfabética de Classes" e procure os nomes correspondentes. Siga qualquer uma delas para observar a documentação de classes documentação de classes. Para a QApplicationQApplication, verá o construtor e todos os outros métodos que esta classe disponibiliza. Se seguir uma hiper-ligação, irá obter mais informação sobre a utilização e significado dos métodos, o que é muito útil quando você às vezes não consegue detectar a utilização correcta ou deseja ver um exemplo. Isto também vale para a documentação da biblioteca KDE, que utiliza um tipo de documentação similar; assim isto é praticamente tudo o que tem de saber sobre a utilização da referência de classes com o navegador de documentação. Interpretação do Exemplo Começando com a QApplicationQApplication, você irá ver todos os métodos utilizados no nosso primeiro exemplo: o construtor QApplicationQApplication(), o método setMainWidget() e o método exec(). A interpretação sobre porque utilizamos estes métodos é muito simples: primeiro criamos uma instância da classe QApplicationQApplication com o construtor, para que possamos utilizar os elementos GUI disponibilizados pela Qt Qt, criamos um widget que será o contentor da janela do nosso programa, definimos o widget como sendo o widget principal para a, executamos a instância a da QApplicationQApplication. O segundo objecto do nosso programa é o botão, como uma instância da classe QPushButton. Dos dois construtores dados para criar uma instância, nós utilizamos o segundo: este aceita um texto, que é a etiqueta do conteudo do botão; aqui, a etiqueta é o texto "Hello world!". Depois chamamos o método resize() para modificar o tamanho do botão de acordo com o seu conteudo- o botão tem de ser maior para tornar o texto completamente visível. Mas e então o método show()? Agora, você vê que tal como a maior parte dos outros widgets, QPushButton é baseado numa herança única- aqui, a documentação diz, Herda QButtonQButton. Siga a hiper-ligação para a classe QButtonQButton. Isto irá mostrar-lhe imensos outros métodos que são herdados pelo QPushButton, que iremos utilizar mais tarde para explicar o mecanismo sinal/espaço. De qualquer modo, o método show() não está listado, pelo que, tem de ser um método que é disponibilizado também por herança. A classe que o QButtonQButton herda, é QWidgetQWidget. Basta seguir de novo a hiper-ligação, e irá ver imensos métodos que a classe QWidgetQWidget disponibiliza; incluindo o método show(). Agora nós compreendemos o que foi feito no exemplo com o botão: criar uma instância do QPushButton, utilizar uma segunda construção para definir o texto do botão, redimensionar o widget para o seu conteudo, definir o widget como sendo o widget principal da instância a da QApplicationQApplication, dizer ao widget para se mostrar no ecrã chamando show(), um método herdado de QWidgetQWidget. Após chamar o método exec(), a aplicação é visível para o utilizador, mostrando uma janela com um botão apresentando "Hello world!". Agora, programas GUI comportam-se de um modo ligeiramenta diferente das aplicações processuais. A coisa principal aqui é que a aplicação entra num chamado "ciclo de evento principal". Isto significa que o programa tem de aguardar por acções do utilizador e depois reagir a estas, também que para uma aplicação Qt Qt, o programa tem de estar no ciclo de evento principal para iniciar o manuseamento de eventos. A secção seguinte diz-lhe de forma breve o que isto significa para o utilizador e o que a Qt Qt oferece para processar eventos do utilizador. (Para utilizadores já avançados: O botão não tem pai declarado no construtor, por isso é por si só um widget de nível superior e corre num ciclo de evento local que não tem de aguardar pelo ciclo de evento principal, veja a documentação de classe documentação de classe QWidgetQWidget e O Guia de Referência da Biblioteca KDE) Sumário: Uma aplicação Qt Qt tem sempre de ter uma instância da classe QApplicationQApplication. Isto permite que possamos criar janelas que são a representação gráfica de programas para o utilizador final e permitem interacção. O conteudo da janela em si é chamado o "Widget Principal", significando que todos os elementos gráficos são baseados na classe QWidgetQWidget e podem ser qualquer tipo de widget que sirva as necessidades da aplicação para comunicar com o utilizador. Assim, todos os elementos de utilizador têm de herdar de uma forma ou de outra QWidgetQWidget para serem visíveis. Interacção do Utilizador Após ler as secções anteriores, você já deverá saber: o que a biblioteca Qt Qt disponibiliza em termos de aplicações GUI, como é que um programa que utilize a Qt Qt é criado e onde e como obter informação sobre classes que deseja utilizar com o navegador de documentação Agora iremos começar a dar "vida" à aplicação através do processamento de eventos do utilizador. Geralmente, o utilizador tem duas formas de interagir com o programa: o rato e o teclado. Para ambas as formas, um interface gráfico de utilizador (GUI) tem de disponibilizar métodos que detectem acções e métodos que façam alguma coisa como reacção a estas acções. O sistema de Janelas assim envia todos os eventos de interacção para a respectiva aplicação. A QApplicationQApplication envia-os então para a janela activa como um QEventQEvent e os prórpios widgets têm de decidir o que fazer com os eventos. Um widget recebe o evento e processa QWidgetQWidget::event(QEventQEvent*)/, que então decide que evento foi executado e como reagir; event() é assim quem faz o manuseamento principal de eventos. Depois, a função event() passa o evento para os chamados filtros de eventos, que determinam que aconteceu e o que fazer com o evento. Se nenhum filtro encontra um responsável pelo evento, os manuseadores especializados de eventos são chamados. Assim podemos decidir entre: a) Eventos de Teclado -- teclas TAB e Shift-TAB: muda o focus de entrada do teclado do widget actual para o widget seguinte na ordem de focus. O focus pode ser atribuido aos widgets chamando setFocusPolicysetFocusPolicy()() e processar os seguintes indicadores de eventos: virtual void focusInEventfocusInEvent() ( QFocusEventQFocusEvent * ) virtual void focusOutEventfocusOutEvent() ( QFocusEventQFocusEvent * ) b) todos os restantes eventos de teclado: virtual void keyPressEventkeyPressEvent() ( QKeyEventQKeyEvent * ) virtual void keyReleaseEventkeyReleaseEvent() ( QKeyEventQKeyEvent * ) c) movimentos do rato: virtual void mouseMoveEvent ( QMouseEventQMouseEvent * ) virtual void enterEvent ( QEventQEvent * ) virtual void leaveEvent ( QEventQEvent * ) d) acções de botões do rato: virtual void mousePressEvent ( QMouseEventQMouseEvent * ) virtual void mouseReleaseEvent ( QMouseEventQMouseEvent * ) virtual void mouseDoubleClickEvent ( QMouseEventQMouseEvent * ) e) eventos da janela que contém o widget: virtual void moveEvent ( QMoveEventQMoveEvent * ) virtual void resizeEvent ( QResizeEventQResizeEvent * ) virtual void closeEvent ( QCloseEventQCloseEvent * ) Note que todas as funções de eventos são virtuais e protegidas; assim você pode re-implementar os eventos que necessitar nos seus próprios widgets e especificar como o seu widget tem de reagir. O QWidgetQWidget também contém alguns outros métodos virtuais que podem ser úteis nos seus programas; de qualquer modo, é suficiente saber sobre o QWidgetQWidget de uma forma geral. Interacção de Objectos através de Sinais e Espaços Agora estamos a chegar às vantagens mais óbvias do conjunto de ferramentas Qt Qt: o mecanismo sinal/espaço. Isto oferece uma solução bastante "à-mão" e útil para interacção de objectos, que é normalmente resolvido através de funções de callback para conjuntos de ferramentas X-Window. Como esta comunicação requer uma programação estrita e às vezes torna a criação de interfaces gráficos muito difícil (como referido pela documentação Qt Qt e explicado em Programação com Qt Qt por K.Dalheimer), a Troll Tech inventou um novo sistema onde objectos podem emitir sinais que podem ser conectados a métodos declarados como espaços. Para a parte C++ do programador, ele apenas tem de saber algumas coisas sobre este mecanismo: a declaração de classe da classe que utiliza sinais/espaços tem de conter a macro Q&_;OBJECT no início (sem o ponto e vírgula); e tem de derivar da classe QObject, um sinal pode ser emitido por uma palavra-chave emit, por ex. emit signal(parâmetros); a partir de qualquer função membra de uma classe que permita sinais/espaços, todos os sinais utilizados pelas classes que não são herdadas têm de ser adicionados à declaração da classe através de uma secção signals:, todos os métodos que podem ser conectados com um sinal são declarados em secções com a palavra-chave adicional slot, por ex. public slots: dentro da declaração da classe, o compilador de meta-objectos moc tem de correr sobre o ficheiro header para expandir as macros e produzir a implementação (que não é necessário saber.). Os ficheiros resultantes do moc são compilados também pelo compilador C++. Outra forma de utilizar sinais sem derivar de QObject é utilizar a classe QSignal- veja a documentação de referência para mais informação e exemplos de utilização. Nos exemplos seguintes, nós assumimos que você está a derivar da QObject. Desta forma, a sua classe é capaz de enviar sinais para qualquer lado e de disponibilizar espaços a onde se possam conectar sinais. Utilizando os sinais, você não tem de se preocupar com quem os está a receber- você apenas tem de emitir o sinal e qualquer que seja o espaço que lhe queira conectar poderá reagir à emissão. Também os espaços podem ser utilizados como métodos normais durante a implementação. Agora, para conectar um sinal a um espaço, tem de utilizar os métodos connect() que são disponibilizados através do QObject ou, onde disponível, métodos especiais que os objectos disponibilizam para definir a conecção para um certo sinal. Exemplo de Utilização Para explicar a forma como definir interacção de objectos, iremos agarrar de novo no nosso primeiro exemplo e extende-lo através de uma conecção simples: #include <qapplication.h> #include <qpushbutton.h> int main( int argc, char **argv ) { QApplication a( argc, argv ); QPushButton hello( "Hello world!" ); hello.resize( 100, 30 ); a.setMainWidget( &&;hello ); connect(&&;hello, SIGNAL( clicked() ), &&;a, SLOT( quit() )); hello.show(); return a.exec(); } Como vê, a única adição para dar ao botão mais interacção é utilizar um método connect(): connect(&&;hello, SIGNAL( clicked() ), &&;a, SLOT( quit() )); é tudo o que temos de acrescentar. Qual é o significado agora? A declaração da classe de QObject diz sobre o método connect(): bool connect ( const QObject * sender, const char * signal, const QObject * receiver, const char * member ) Isto significa, você tem de especificar um ponteiro de uma instância de QObject que é o emissor do sinal, significando que pode emitir este sinal como primeiro parâmetro; depois você tem de especificar o sinal a que se deseja conectar. Os dois últimos parâmetros são o objecto receptor que disponibiliza um espaço, seguido pela função membro que é na realidade o espaço que será executado aquando da emissão do sinal. Utilizando sinais e espaços, os objectos do seu programa podem interagir entre eles facilmente sem dependerem explicitamente do tipo de objecto receptor. Você irá aprender mais sobre a utilização deste mecanismo para utilização produtiva mais tarde neste manual. Mais informação sobre o mecanismo Sinal/Espaço pode ser encontrado no Guia de Referência da Biblioteca KDE e na referência electrónica Qt Qt. O que o KDE disponibiliza As bibliotecas KDE 1.1.x À altura deste documento e devido ao facto de o KDevelop utilizar KDE 1.1, referimo-nos ao estado das bibliotecas KDE bibliotecas KDE nessa distribuição. As bibliotecas KDE bibliotecas KDE principaos que irá utilizar para criar as suas próprias aplicações KDE aplicações KDE são: a biblioteca KDE-Core, contendo todas as classes que são elementos não-visíveis e disponibilizam funcionalidades que a sua aplicação pode utilizar. a biblioteca KDE-UI, contendo elementos de interface com o utilizador tais como barras de menu, de ferramenta ferramentas e afins, a biblioteca KFile, contendo diálogos de selecção de ficheiros, Adicionalmente, para soluções específicas o KDE oferece as seguintes bibliotecas: a biblioteca KHTMLW, oferecendo um completo widget de interpretação HTML que é utilizado por vários programas como o KDEHelp KDEHelp, KFM, KDevelop, a biblioteca KFM, permitindo a utilização do gestor de ficheiros do KDE a partir de dentro da sua aplicação. a biblioteca KAb, o KAddressBook. Disponibiliza acesso a livro de endereços para por ex. aplicações de e-mail a biblioteca KSpell, oferecendo widgets e funcionalidades para integrar a utilização do Ispell, o corrector ortográfico comum, em aplicações tais como editores; utilizado na aplicação KEdit. De seguida, iremos observar o que é necessário para tornar a nossa primeira aplicação Qt Qt numa KDE. Aplicação KDE Exemplo No seguinte código, irá ver que escrever uma aplicação KDE não é muito mais difícil que uma aplicação Qt Qt. Para a utilização das características do KDE, apenas tem de utilizar algumas outras classes, e está quase tudo feito. Como um exemplo, iremos discutir a versão alterada do exemplo Qt Qt dado acima: #include <kapp.h> #include <qpushbutton.h> int main( int argc, char **argv ) { KApplication a( argc, argv ); QPushButton hello( "Hello world!" ); hello.resize( 100, 30 ); a.setTopWidget( &&;hello ); connect(&&;hello, SIGNAL( clicked() ), &&;a, SLOT( quit() )); hello.show(); return a.exec(); } Pode ver que primeiro alteramos de QApplicationQApplication para KApplicationKApplication. Além disso, tivemos de modificar o método setMainWidget() anteriormente utilizado para setTopWidget, que a KApplicationKApplication utiliza para definir o widget principal. E é tudo! A sua primeira aplicação KDE está pronta- apenas tem de dizer ao compilador o caminho dos ficheiros include do KDE e ao linker para ligar a biblioteca KDE-Core com -lkdecore. Como você agora saber o que pelo menos a função main() disponibiliza de forma geral e como uma aplicação se torna visível e permite a interacção de utilizadores e objectos, iremos continuar com o capítulo seguinte, onde a nossa primeira aplicação é feita com o KDevelop- aí poderá também testar tudo o que foi mencionado anteriormente e verificar os resultados. O que deverá ter consultado adicionalmente até agora é a documentação de referência da Qt Qt, especialmente as classes QApplicationQApplication, QWidgetQWidget e QObject e a documentação da biblioteca KDE-Core para a classe KApplicationKApplication. O manual de Referência da Biblioteca KDE também cobre uma descrição completa sobre a invocação dos construtores QApplicationQApplication e KApplicationKApplication incluindo processamento de argumentos de linha de comandos. Criar Novas Aplicações O KAppWizard, ou também denominado Assistente de Aplicações KDE, destina-se a permitir-lhe começar a trabalhar em novos projectos com o KDevelop. Assim, todos os seus projectos são primeiro criados através do assistente; depois você pode começar a construi-los e extender o código do esqueleto já disponibilizado. O KAppWizard também permite escolher entre vários tipos de projectos de acordo com os objectivos do seu projecto: Aplicação KDE Normal: inclui código fonte para uma moldura de estrutura completa de uma aplicação KDE standard com suporte para o modelo Controlador-de-Vista-de-Documento, uma barra de menu, ferramentas ferramentas e estados bem como um conjunto de documentação baseado em SGML SGML, hiper-ligações KDE e icons de aplicações. Este é o tipo de aplicação normalmente necessária para um novo projecto KDE. Mini-Aplicação KDE: contém a mesma estrutura geral que a Aplicação KDE Normal mas com a diferença que o código da aplicação apenas disponibiliza um widget como uma janela. Aplicação Qt Qt Normal: trabalha como a Aplicação KDE Normal através do seu código com a diferença de que o projecto é baseado na biblioteca Qt Qt apenas e não contém suporte KDE. É destinado a projectos que têm de ser portáveis para plataformas Windows ou que não querem requerer as bibliotecas KDE bibliotecas KDE por parte do utilizador final. Aplicação C: É baseada apenas no compilador C e corre numa consola. Aplicação C++: É baseada apenas no compilador C++ e corre numa consola tal como a aplicação C, pelo que não requer o X-Window. Projecto Costumizável: cria um projecto vazio sem qualquer código fonte. Este destina-se a projectos já existentes para serem convertidos para o KDevelop ou para projectos onde deseja começar de raiz. Lembre-se que tem de tomar conta dos ficheiros makefiles e dos scripts de configuração sozinho. Neste capítulo veremos como o KAppWizard pode ser invocado e o que tem de ser feito para gerar um projecto de aplicação KDE. Isto irá ser também o passo inicial da nossa cobertura, onde iremos criar a versão inicial do projecto de exemplo. Para todos os outros tipos de projectos os passos são normalmente os mesmos, apenas que poderá não necessitar de ter certas opções disponíveis. Invocar o KAppWizard e a Geração do Projecto Iniciar o KAppWizard e a Primeira Página Para começar com a sua primeira aplicação KDE, abra o KDevelop. Depois seleccione "Novo..." a partir do menu "Projecto". O KAppWizard inicia-se, e você vê uma árvore na primeira página, contendo os tipos de projectos. Quando um tipo é seleccionado, você vê uma antevisão de como irá funcionar após o processo inicial de construção. Escolha o ramo KDE, tipo Normal. Depois prima o botão "Seguinte" no fundo da primeira página do assistente. Isto mudará para a página seguinte, onde você tem de definir as opções gerais do projecto. A Página de Geração de Definições Para a nossa aplicação de exemplo, nos escolhemos o nome de projecto KScribble; pelo que deverá inserir isto no campo "Nome do Projecto". Depois seleccione o directório onde quer que o projecto seja construido; o defeito é o seu directório principal (home). Pode introduzir o caminho manualmente ou pode também premir o botão à direita para seleccionar o directório através de um diálogo. De seguida, tem de introduzir o número da Versão. Para a primeira versão, defina esta como 0.1. É normal numerar novas aplicações que estão em desenvolvimento para a primeira distribuição abaixo de 1, e como a versão inicial irá apenas conter a estrutura standard, diremos que esta é a versão 0.1. Finalmente, adicione o seu nome ao campo "Autor" e o seu endereço de e-mail. Pode deixar todas as outras opções nos seus valores de defeito. Para lhe dar algumas informações sobre outras opções, pode premir o botão direito do rato sobre as opções, e obterá uma janela de ajuda rápida que descreve o objectivo da opção. Estes são: gerar código e headers: gera o código fonte da aplicação Ficheiros-Standard-GNU: adiciona uma cópia da Licença Pública Geral (GPL) GNU ao projecto bem como alguns outros ficheiros standard para informação do utilizador quando distribuir o pacote. Documentação do Utilizador: o manual do utilizador em SGML SGML, já preparado para o seu projecto. Documentação-API API: cria um conjunto de documentação HTML inicial para o Interface de Programação da Aplicação (API). Ficheiro-lsm: o Mapa de Aplicações Linux, utilizado pelos servidores de distribuição e contém uma breve informação sobre os objectivos do projecto e requerimentos. Ficheiro-.kdelnk .kdelnk: um atalho KDE que irá instalar a sua aplicação na árvore "Aplicações" no Painel de Controlo KDE. Icon de Programa: um Icon que representa o seu projecto e pode ser utilizado para criar um atalho para o ambiente de trabalho. Mini-Icon: uma versão-Mini do icon de programa que representa a sua aplicação para além do nome dela no Painel de Controlo KDE e é mostrado nos diálogos e janela principal da sua aplicação no canto superior esquerdo. Agora vamos para a página seguinte premindo o botão "Seguinte" de novo para definir o modelo para os ficheiros header do seu projecto. Os Modelos de Header e Código A página de modelo de header permite-lhe incluir automaticamente um prefácio nos seus ficheiros header, contendo o nome do ficheiro, a data de construção, o ano do copyright, também o seu nome e endereço de e-mail. Você não tem de mudar essas partes em maiusculas por si, pois o KAppWizard faz isto automaticamente e guarda o modelo para este projecto, para que possa ser utilizado de novo mais tarde para criar novos ficheiros. A segunda parte do modelo de header de defeito contém uma informação de licença. Por defeito, o seu projecto está sob a Licença Pública Geral GNU, que também é incluida no pacote. Esta licença é utilizada para proteger o seu código fonte contra qualquer pessoa que apenas copie o seu código para os seus propósitos. A Licença Pública Geral (GPL) oferece-lhe esta licença gratuitamente e assim protege os seus direitos como autor, e é comum para aplicações de distribuição livre. Para obter mais informação sobre a licença, deverá ler mais tarde o ficheiro COPYING no directório base do seu novo projecto que é uma cópia da GPL e é já enviada com a sua aplicação. De qualquer modo, você pode escolher outra licença ou outro modelo de header que já esteja a utilizar para os seus projectos. Sendo assim você pode editar directamente o modelo de defeito dado. Para fazer isto, é-lhe dado o modelo numa janela de edição. Para limpar a página de defeito, seleccione "Novo", para utilizar outro modelo, seleccione "Ler...", que lhe permite escolher um ficheiro de modelo. Quando tiver terminado, vá para a página seguinte premindo "Seguinte". Esta é a página de modelo para os seus ficheiros de código fonte e é geralmente a mesma que a página de modelo de header. A única diferença é que este modelo é utilizado para os seus ficheiros de implementação. Criar o Projecto Agora que já definiu todas as opções para o KScribble, seleccione "Seguinte" e prima o botão "Gerar" no fundo da janela do assistente. Se o botão não estiver disponível, você não definiu todas as opções correctamente. Para corrigir quaisquer erros, volte atrás no Assistente com "Regressar". Depois você verá o que o KAppWizard faz- ele copia todos os modelos para o directório do seu projecto e cria o novo projecto. Após o KAppWizard terminar, o botão "Cancelar" muda para um botão "Sair" para deixar o assistente. Após este último passo, você terminou a criação do novo projecto. O KDevelop lê-o então e a vista de árvore permite-lhe navegar através ficheiros e classes do projecto. Na próxima secção, iremos discutir como construir e correr a sua primeira versão do KScribble e como o código fonte está organizado. A Primeira Construção Após o seu projecto ser gerado, iremos primeiro fazer uma viagem através do código fonte para obter um entendimento geral de como funciona o esqueleto da aplicação. Isto não só ajudará a começar como também saberemos onde mudar o quê em passos futuros. Quando abrimos a página do VLF (Visualizador Lógico de Ficheiros) na vista de árvore, você vê algumas pastas que já ordenam os ficheiros do projecto relevantes para o programador. As duas primeiras pastas são "Header" e "Código". A pasta-Header assim sendo contém logicamente todos os ficheiros header do projecto, a pasta-Código todo o código fonte. Todas as outras pastas não são de interesse por agora, pelo que voltaremos aqui mais tarde para ver o que contêm. Essas duas pastas contêm então os seguintes ficheiros: Headers: kscribble.h : contém a declaração de classe para a classe KScribbleApp. kscribbledoc.h : contém a declaração de classe para a classe KScribbleDoc. kscribbleview.h : contém a declaração de classe para a classe KScribbleView. resource.h : contém uma colecção de macro para os menu-ID's Código: kscribble.cpp : contém a implementação da classe KScribbleApp. kscribbledoc.cpp : contém a implementação da classe KScribbleDoc. kscribbleview.cpp : contém a implementação da classe KScribbleView. main.cpp : contém a implementação da função main(). Mas antes de mergulharmos no código, vamos deixar o KDevelop construir e correr a nossa nova aplicação. Para fazer isto, seleccione "Make" a partir do menu "Construir" ou prima o botão correspondente na barra de ferramentas barra de ferramentas. A janela de output abre na base do KDevelop e deixa-o ver o que o make está a fazer através das mensagens que nos dá: 1 Making all in docs 2 make[1]: Entering directory `/home/rnolden/Tutorial/kscribble1/kscribble/docs' 3 Making all in en 4 make[2]: Entering directory `/home/rnolden/Tutorial/kscribble1/kscribble/docs/en' 5 make[2]: Nothing to be done for `all'. 6 make[2]: Leaving directory `/home/rnolden/Tutorial/kscribble1/kscribble/docs/en' 7 make[2]: Entering directory `/home/rnolden/Tutorial/kscribble1/kscribble/docs' 8 make[2]: Nothing to be done for `all-am'. 9 make[2]: Leaving directory `/home/rnolden/Tutorial/kscribble1/kscribble/docs' 10 make[1]: Leaving directory `/home/rnolden/Tutorial/kscribble1/kscribble/docs' 11 make[1]: Entering directory `/home/rnolden/Tutorial/kscribble1/kscribble' 12 g++ -DHAVE_CONFIG_H -I. -I. -I.. -I/opt/kde/include -I/usr/lib/qt/include -I/usr/X11R6/include -O0 -g -Wall -c kscribbleview.cpp 13 g++ -DHAVE_CONFIG_H -I. -I. -I.. -I/opt/kde/include -I/usr/lib/qt/include -I/usr/X11R6/include -O0 -g -Wall -c kscribbledoc.cpp 14 g++ -DHAVE_CONFIG_H -I. -I. -I.. -I/opt/kde/include -I/usr/lib/qt/include -I/usr/X11R6/include -O0 -g -Wall -c kscribble.cpp 15 g++ -DHAVE_CONFIG_H -I. -I. -I.. -I/opt/kde/include -I/usr/lib/qt/include -I/usr/X11R6/include -O0 -g -Wall -c main.cpp 16 /usr/bin/moc ./kscribble.h -o kscribble.moc.cpp 17 g++ -DHAVE_CONFIG_H -I. -I. -I.. -I/opt/kde/include -I/usr/lib/qt/include -I/usr/X11R6/include -O0 -g -Wall -c kscribble.moc.cpp 18 /usr/bin/moc ./kscribbledoc.h -o kscribbledoc.moc.cpp 19 g++ -DHAVE_CONFIG_H -I. -I. -I.. -I/opt/kde/include -I/usr/lib/qt/include -I/usr/X11R6/include -O0 -g -Wall -c kscribbledoc.moc.cpp 20 /usr/bin/moc ./kscribbleview.h -o kscribbleview.moc.cpp 21 g++ -DHAVE_CONFIG_H -I. -I. -I.. -I/opt/kde/include -I/usr/lib/qt/include -I/usr/X11R6/include -O0 -g -Wall -c kscribbleview.moc.cpp 22 /bin/sh ../libtool --silent --mode=link g++ -O0 -g -Wall -o kscribble -L/opt/kde/lib -L/usr/X11R6/lib -rpath /opt/kde/lib -rpath /usr/X11R6/lib kscribbleview.o kscribbledoc.o kscribble.o main.o kscribble.moc.o kscribbledoc.moc.o kscribbleview.moc.o -lkfile -lkfm -lkdeui -lkdecore -lqt -lXext -lX11 23 make[1]: Leaving directory `/home/rnolden/Tutorial/kscribble1/kscribble' Como pode ver, numeramos todas as linhas, o que não irá surgir no seu output; apenas torna mais fácil descrever agora o que aconteceu durante a construção. Primeiro que tudo, o make trabalha recursivamente. Isto é, comça no directório em que foi invocado e então vai para os subdirectórios primeiro, regressa e processa o subdirectório seguinte. Finalmente, o directório em que foi iniciado é processado e o make termina. Assim, o make começou primeiro no directório principal do projecto contendo o código. Nas linhas 1 e 2, você vê como o processo make entra no directório docs, depois no subdirectório en. Como não existe nada para fazer, ele deixa estes directórios até regressar ao directório fonte kscribble na linha 11. Depois, o verdadeiro trabalho começa: o make invoca o compilador, aqui o g++ para compilar o ficheiro de código kscribbleview.cpp. A macro -DHAVE&_;CONFIG&_;H diz que o ficheiro config.h deverá ser utilizado. Este é um ficheiro que contém macros para a plataforma e aplicação específicas e está localizado no directório principal do projecto. Os comandos -I seguintes adicionam o caminho include onde o g++ pode encontrar os includes de que necessita. O caminho é o directório corrente, o directório principal do projecto (através de -I..) e o caminho include para o KDE, Qt Qt e ficheiros header da biblioteca X11. Os directórios para estes ficheiros include foram determinados pelo script configure e definidos nos Makefiles, pelo que, o compilador sabe onde estes se encontram. Finalmente, -O0 define a optimização para zero (sem optimização), -g permite a depuração, -Wall define os avisos do compilador para todos e -c diz ao compilador para produzir um ficheiro de objecto, ou seja apenas compilar o ficheiro. Isto é feito também para os outros ficheiros de código do nosso projecto nas linhas 13-15. Obviamente, o nosso código é compilado, mas em vez de ligar os ficheiros objecto do código ao binário final, nós vemos alguns outros comandos. Na linha 16, você vê que o programa "moc" é chamado para processar o ficheiro header kscribble.h, com o seu resultado em kscribble.moc.cpp. Depois, na linha 17, este ficheiro de código é também compilado. O mesmo acontece com os outros ficheiros header do projecto até à linha 21. Agora, como o conjunto de ferramentas Qt Qt contém o mecanismo sinal/espaço, mas continua a ser uma implementação C++, você está a utilizar algumas palavras-chave que não são originalmente linguagem C++, tais como as declarações signals: e slots: nas suas classes. Isto dá-lhe a possibilidade de permitir facilmente comunicação entre objectos para todas as classes de objectos que herdem a classe QObject, pelo que pode evitar os normais ponteiros para funções de chamada (callback). Assim, a aplicação necessita do código que implementa esta funcionalidade, e é por isto que o moc é chamado. Moc é o Compilador de Objectos Meta (Meta Object Compiler) do conjunto de ferramentas Qt Qt e constroi a implementação para mecanismos de sinais e espaços percorrendo os ficheiros header e produzindo um código de resultado que tem de ser compilado no binário. Como os projectos do KDevelop utilizam o automoc para determinar, que ficheiro header necessita de ser processado, você não tem de se preocupar com nenhuma chamada ao moc nem com o compilador C++ no ficheiro moc de resultado. Lembre-se apenas da regra que faz uma classe utilizar os sinais e espaços- hereditariedade da QObject ou qualquer classe que herde em si a QObject, inclusão da macro Q&_;OBJECT (sem ponto e vírgula !) no início da declaração de classe e a declaração para os sinais e espaços. Finalmente, o seu binário é construido pelo compilador. O binário de resultado é chamado kscribble, o linkador inclui o caminho para as bibliotecas KDE e X11 e liga o código contra as bibliotecas kfile, kfm, kdeui, kdecore, qt, Xext e X11. Depois tudo está feito e o make termina. O Esqueleto de Código Para obter um conceito de como uma aplicação KDE funciona, teremos primeiro que olhar muito atentamente para o esqueleto de código já disponibilizado pelo Assistente de Aplicação. Como nós já vimos, temos um conjunto de ficheiros de código e header que constroem o código inicial para a aplicação e a tornam pronta-a-correr. Assim, a maneira mais fácil de explicar o código é seguir a implementação linha a linha como é processado durante a execução do programa até entrar no ciclo principal de evento e está pronto a receber interacção do utilizador. Depois, iremos observar a functionalidade que permite a interacção do utilizador e como certas coisas funcionam. Esta é provavelmente a melhor forma de explicar o esqueleto e, como é similar a quase todas as aplicações KDE aplicações KDE, irá permitir-lhe ler também o código fonte de outros projectos; adicionalmente, você saberá aqui onde alterar que parte do código para fazer com que as suas aplicações se comportem da forma para que foram desenhadas. A Função <literal remap="tt">main()</literal> Como a aplicação inicia a sua execução entrando na função main(), este será o ponto de partida do nosso exame ao código. A função main() do KScribble é implementada no ficheiro main.cpp e pode também ser encontrada utilizando o Navegador de Classes seleccionando a pasta "Globais", sub-pasta "Funções": 1 #include "kscribble.h" 2 3 int main(int argc, char* argv[]) { 4 KApplication app(argc,argv,"KScribble"); 5 6 if (app.isRestored()) 7 { 8 RESTORE(KScribbleApp); 9 } 10 else 11 { 12 KScribbleApp* kscribble = new KScribbleApp; 13 kscribble->show(); 14 if(argc > 1){ 15 kscribble->openFile(argv[1]); 16 } 17 } 18 return app.exec(); 19 } Agora, o que acontece primeiro é a normal criação do objecto KApplicationKApplication, que obtém o nome da nossa aplicação KScribble como terceiro parâmetro. Quando se cria um novo KApplicationKApplication, uma nova instância de KConfigKConfig é criada também que é conectada ao ficheiro de configuração em $HOME/.kde/share/config/appname + rc que guarda toda a informação que queremos utilizar quando iniciamos janelas da aplicação. O nome que passamos ao construtor de app será utilizado como o título da janela mais tarde. Apesar do código de exemplo para tornar a primeira aplicação Qt Qt numa KDE, o código seguinte é ligeiramente diferente. Após o objecto KApplicationKApplication estar presente, nós testamos se a aplicação é iniciada através do gestor de sessões do kwm ou manualmente através do utilizador. Isto pode ser descoberto quando se chama isRestored() no objecto app, que retorna true (verdade) para gestão de sessão e false (falso) para um início normal. Como a gestão de sessão é uma característica principal das aplicações KDE aplicações KDE e vastamente utilizada pelo esqueleto mas com muito mais para explicar, nós vamos seguir a secção else&{;&}; primeiro; depois voltaremos e explicaremos a funcionalidade de sessão num passo seguinte. Aplicação Iniciada pelo Utilizador A secção else&{;&}; agora cria uma instância da classe KScribbleApp na linha 12. Este objecto é chamado para se mostrar a sí próprio na linha 13 como normal; a linha 14 determina se um argumento de linha de comando foi passado e, como isto é normalmente o nome de um ficheiro, chama o objecto kscribble para o abrir com openFile(). Note que nós não chamamos o método setTopWidget(kscribble) para a nossa aplicação- isto já foi feito pela classe que o KScribbleApp herda. Agora vamos observar o nosso objecto KScribbleApp- o que é e o que é que já disponibiliza? A única coisa que nós sabemos até agora é que tem de ser um Widget para representar o interface do utilizador na janela principal. Vamos ver a implementação da classe de KScribbleApp, que pode ser encontrada no ficheiro kscribble.cpp ou através de um clique no icon da classe no Navegador de Classes. Como a instância é criada pelo construtor. Primeiro que tudo, vemos que herda a classe KTMainWindowKTMainWindow, que é parte da biblioteca kdeui. Esta classe em sí herda QWidgetQWidget, pelo que, como normal, nós temos um widget normal como sendo a janela de topo. O KTMainWindowKTMainWindow contém imensas functionalidades de que a classe KScribbleApp tira proveito. Disponibiliza barras de menu, ferramentas ferramentas, estados e suporte para gestão de sessões. A única coisa que temos que fazer quando a criar sub-classes da KTMainWindowKTMainWindow é criar todos os objectos de que necessitamos e criar outro widget que é gerido pela instância KTMainWindowKTMainWindow como a vista principal no centro da janela; normalmente este é o local onde o utilizador trabalha tal como numa vista de edição de texto. O Construtor Vamos observar o código para o construtor e ver como a instância é criada: 1 KScribbleApp::KScribbleApp() 2 { 3 config=kapp->getConfig(); 4 5 6 /////////////////////////////////////////////////////////////////// 7 // chama inits para invocar todas as outras partes de construção 8 initMenuBar(); 9 initToolBar(); 10 initStatusBar(); 11 initKeyAccel(); 12 initDocument(); 13 initView(); 14 15 readOptions(); 16 17 /////////////////////////////////////////////////////////////////// 18 // desactiva itens de menu e barra de ferramentas no arranque 19 disableCommand(ID_FILE_SAVE); 20 disableCommand(ID_FILE_SAVE_AS); 21 disableCommand(ID_FILE_PRINT); 22 23 disableCommand(ID_EDIT_CUT); 24 disableCommand(ID_EDIT_COPY); 25 disableCommand(ID_EDIT_PASTE); 26 } Nós vemos que a nossa instância de configuração do KConfigKConfig aponta agora para a configuração da aplicação, pelo que podemos operar mais tarde com as entradas de configuração do ficheiro. Depois, todas as partes da aplicação que são necessárias são criadas pela sua correspondente função membro que é especificada na nossa janela principal: initMenuBar(): constroi a barra de menu, initToolBar(): constroi a barra de ferramentas ferramentas, initStatusBar(): cria a barra de estados, initKeyAccel(): define todos os atalhos de tecladoaccelerator para a nossa aplicação através da configuração de teclado global e específica da aplicação initDocument(): cria o objecto documento para a janela da aplicação initView(): cria o widget principal para a nossa vista dentro da janela principal readOptions(): lê todas as definições específicas da aplicação a partir do ficheiro de configuração e inicializa o resto da aplicação tal como a lista de ficheiros recentes, as posições das barras e tamanho da janela. Finalmente, nós desactivamos alguns comandos que o utilizador pode realizar, porque não podem estar disponíveis no estado actual da aplicação. Como temos agora uma visão genérica de como a janela da aplicação é criada, iremos olhar para os detalhes de como os elementos do utilizador são construidos seguindo os métodos acima. A Barra de Menu Como mostrado acima, a barra de menu do KScribble é criada pelo método initMenuBar(). Aí, nós criamos um conjunto de QPopupMenuQPopupMenus que surgem quando o utilizador selecciona uma entrada de menu. Depois, nós inserimo-las na barra de menu e conectamo-las com as entradas. Primeiro, nós criamos o nosso recent&_;file&_;menu, que irá conter os nomes dos últimos 5 ficheiros abertos. Temos de fazer isto primeiro, porque esta entrada de menu é inserida no file&_;menu. Quando adicionamos a conecção directamente- apenas obtemos o sinal que é emitido pela entrada de menu com o seu número de entrada e chamamos a slotFileOpenRecent( int ), que então chama o ficheiro correcto a partir da lista de ficheiros recentes para ser aberto. Depois criamos o nosso menu "Ficheiro". Este será o menu que irá ser visível na barra de menu. As acções standard são então inseridas no menu de popup uma a uma- primeiro os comandos para criar um novo ficheiro, abrir um ficheiro, fechar um ficheiro etc., finalmente "S&&;ir" para fechar a aplicação. Todas as entradas de menu têm de ser criadas na ordem em que surgem mais tarde, pelo que temos de manter um olho no que queremos ter em que lugar. Como um exemplo, nós olhamos para as seguintes entradas: file_menu->insertItem(Icon("fileopen.xpm"), i18n("&&;Open..."), ID_FILE_OPEN ); file_menu->insertItem(i18n("Open &&;recent"), recent_files_menu, ID_FILE_OPEN_RECENT ); A primeira insere a entrada "Abrir...". Como nós queremos te-la como um icon, nós utilizamos o método insertItem() com o nome do icon. Para entender o processo de leitura do icon, nós necessitamos de saber o que ou onde Icon() é declarada- de facto, é uma macro disponibilizada pela classe KApplicationKApplication: #define Icon(x) kapp->getIconLoader()->loadIcon(x) Adicionalmente, utiliza a seguinte macro internamente para obter acesso ao objecto da aplicação: #define kapp KApplication::getKApplication() Isto significa que o objecto KApplicationKApplication já contém uma instância de um leitor de Icons- nós apenas temos de ter acesso a ela; depois irá ler o icon correspondente. Como os nossos icons são todos das bibliotecas KDE bibliotecas KDE, nós não temos de nos preocupar com mais nada- eles são instalados no sistema automaticamente, pelo que também não temos de os incluir no pacote da nossa aplicação para os utilizar. Após o parâmetro do icon (que é opcional), nós inserimos o nome da entrada de menu através de i18n("&&;Abrir..."). Aí, nós temos de observar duas coisas: primeiro, a entrada é inserida com o método i18n(). Tal como a entrada Icon(), é uma macro também definida em kapp.hkapp.h e chama o objecto KLocale da KApplicationKApplication para traduzir a entrada para o idioma actualmente utilizado: #define i18n(X) KApplication::getKApplication()->getLocale()->translate(X) Chegando aqui, deverá ser mencionado que uma pessoa poderia pensar "Eu não quero utilizar macros"- você pode fazer isso a maior parte dos casos. Mas aqui é imprescindível a utilização da i18n() porque para a internacionalização os ficheiros de idioma respectivos têm de ser construidos. Como este processo de construção depende da string (conjunto de caracteres) i18n, você tem de utilizar a macro. Como já deve ter adivinhado, o & (i comercial) dentro das entradas de menu é mais tarde interpretado como uma linha por baixo da letra seguinte na entrada de menu. isto permite um acesso rápido ao comando de menu através do teclado quando o utilizador prime a tecla Alt em conjunto com a letra sublinhada. Finalmente, nós damos um ID (número identificativo) à entrada de menu, que é um número inteiro através do qual podemos encontrar a entrada mais tarde. Para manter um controlo sobre os valores utilizados, estes são definidos através de macros e são coleccionados no ficheiro resource.h dentro do seu projecto. Por consistência, estas macros são todas maiúsculas e começam por ID&_;, depois o nome do menu seguido pela entrada. Isto torna muito fácil lembramo-nos do sentido de cada entrada em qualquer ponto do código, pelo que não temos de voltar à implementação da barra de menu de novo para observar as entradas. A segunda entrada de exemplo mostra outra variante do método insertItem(). Aqui, nós acrescentamos o menu de popup recent&_;files&_;menu como um item de menu. Isto significa, que a entrada mostra-se a sí própria com a string dada "Abrir recentes", seguida de uma seta para a direita. Na selecção, o menu de popup de ficheiros recentes surge e o utilizador pode escolher o último ficheiro. Por fim mas não menos importante existem muitas outras formas de inserir items de menu- o esqueleto de aplicação mantém isto o mais simples possível. Mais informação pode ser obtida na documentação Qt Qt sobre a classe QMenuDataQMenuData. Agora, após termos criado os menus de popup file&_;menu, edit&_;menu e view&_;menu, temos de incluir também um menu "Ajuda". Nós podiamos fazer isto tal como os outros, mas a classe KApplicationKApplication oferece um método mais rápido e agradável para cobrir isto: help_menu = kapp->getHelpMenu(true, i18n("KScribble\n" VERSION )); Isto é tudo o que temos de fazer para obter um menu de ajuda que contenha uma entrada para o conteudo da ajuda com o atalhoshortcuts de teclado F1, uma caixa-sobre para a aplicação e uma caixa-sobre para o KDE (que pode ser desactivada chamando getHelpMenu(false,...);). O conteudo para a caixa-sobre da nossa aplicação é definida de novo com a string i18n() - VERSION fica com a macro que é definida para a numeração de versão do projecto no ficheiro config.h, pelo que não temos de modificar isto manualmente de cada vez que queremos fazer uma nova distribuição. Esteja à vontade para adicionar aqui qualquer informação sobre a sua aplicação, por ex. o seu nome, endereço email, copyright e afins. Agora apenas temos de inserir os pop-ups na barra de menu. Como o KTMainWindowKTMainWindow já constroi uma barra de menu para nós, nós apenas os inserimos chamando menuBar()->insertItem();. O que resta fazer é conectar as entradas de menu com os métodos que irão executar. Assim, nós conectamos cada menu de popup através do seu sinal activated( int ) a um método commandCallback( int ), que contém uma frase switch que chama o método correspondente para as entradas de menu. Adicionalmente, nós conectamos os pop-ups através do seu sinal highlighted( int ) para disponibilizar ajuda de barra de estados para cada entrada. Sempre que o utilizador move o seu rato ou focus de teclado focus de teclado para uma entrada, a barra de estados mostra então a correspondente mensagem de ajuda. Após termos terminado com a barra de menu, podemos continuar com a barra de ferramentas barra de ferramentas na secção seguinte. Lembre-se que uma instância de KTMainWindowKTMainWindow apenas pode ter uma barra de menu visível de cada vez; assim se deseja construir diversas barras de menu, você tem de cria-las separadamente com instâncias de KMenuBarKMenuBar e definir uma delas através dos métodos correspondentes de KTMainWindowKTMainWindow como sendo a barra de menu actual. Veja a documentação de classes documentação de classes da KMenuBarKMenuBar para informação mais detalhada sobre como extender as características, veja também Configurar Barras de Menu e Ferramentas. A Barra de Ferramentas A criação de barras de ferramenta ferramentas é agora ainda mais simples que a das barras de menu. Como a KTMainWindowKTMainWindow já disponibiliza barras de ferramenta ferramentas, que são criadas pela primeira inserção, você é livre de criar várias. Basta adicionar os botões para as funções que deseja disponibilizar: toolBar()->insertButton(Icon("filenew.xpm"), ID_FILE_NEW, true, i18n("New File") ); Isto adiciona um botão alinhado à esquerda com o icon "filenew.xpm" com o respectivo ID para a barra de ferramentas ferramentas. O terceiro parâmetro decide se o botão deverá estar activo ou não; por defeito nós definimos isto para true (verdade), porque o nosso método disableCommand() no final do construtor faz isto por nós automaticamente tanto para as entradas do menu como da barra de ferramentas ferramentas. Finalmente, o último parâmetro é utilizado como uma "Dica Rápida"- quando o utilizador move o ponteiro do rato sobre o botão de forma a ficar iluminado, uma pequena janela surge que contém uma pequena mensagem de ajuda, cujo conteudo pode ser definido aqui. Finalmente, todos os botões da barra de ferramentas barra de ferramentas estão conectados novamente ao nosso método commandCallback() através do seu sinal clicked(). Ao sinal pressed(), nós permitimos ao utilizador receber a mensagem de ajuda correspondente na barra de estados. Informação Adicional: Como as barras de ferramentas barras de ferramentas são criadas utilizando a classe KToolBarKToolBar, você deverá ler a respectiva documentação. Com a KToolBarKToolBar, imensas coisas necessárias na barra de ferramentas barra de ferramentas podem ser realizadas tais como pop-ups demorados se o seu botão deseja fazer surgir um menu quando o botão é mantido premido ou até mesmo widgets como caixas de escolha. Também, por defeito, a barra de ferramentas barra de ferramentas preenche a largura total da janela, o que a faz ter um aspecto melhor utilizando apenas uma barra. Quando se usa mais do que uma, você deverá também pensar na hipótese de definir o tamanho da barra para terminar a seguir ao botão mais à direita, para que outras barras possam ser apresentadas na mesma linha por baixo da barra de menu. Iremos discutir algumas tecnicas sobre desing e extensão de barras de ferramentas barras de ferramentas na secção Configurar Barras de Menu e Ferramentas. A Barra de Estados A barra de estados é, bem como as outras barras, já disponibilizada pela instância KTMainWindowKTMainWindow, pelo que apenas temos de inserir os nossos itens como quisermos. Por defeito, o esqueleto contém apenas uma entrada que mostra a ajuda de barra de estados. Para muitas aplicações isto pode não chegar; então você inserirá as entradas que necessite para mostrar por ex. coordenadas e afins. Também, uma aplicação pode apenas ter uma barra de estados de cada vez como as barras de menu. Se desejar construir várias, deverá cria-las separadamente e definir a barra corrente através do método correspondente da KTMainWindowKTMainWindow. A barra de estados também permite inserir widgets, que podem ser utilizador para produzir bons hábitos de apresentar barras de progresso como o KDevelop faz. Verifique a documentação de classes documentação de classes da KStatusBar. Atalhos de Teclado Ao atingir o método initKeyAccel(), já construimos os itens standard da janela principal de uma aplicação- as barras de menu, ferramentas ferramentas e estados. Na verdade, não definimos nenhuns atalhos de teclado através dos quais utilizadores avançados que apenas queiram trabalhar com o teclado possam aceder rapidamente a certos comandos que são utilizados mais frequentemente durante a utilização do programa. Para o fazer, nós podiamos ter inserido as teclas de atalhos na inserção dos itens de menu por exemplo, mas o KDE oferece uma boa solução para construir e manter atalhos de teclado. Imensos utilizadores querem te-los configuráveis por um lado e por outro os atalhos standard deverão ser os mesmos em todas as aplicações. Assim, o Centro de Controlo do KDE permite configurar atalhos de teclado standard globalmente através da utilização da classe KAccelKAccel. Adicionalmente, as bibliotecas KDE bibliotecas KDE contêm um widget que permite aos utilizadores configurar atalhos de teclado específicos da aplicação facilmente. Como o esqueleto da aplicação apenas utiliza itens de menu que têm acções standard tais como "Novo" ou "Sair", estes são definidos pelo método initKeyAccel(). Acções standard apenas têm de ser conectadas, para os valores de teclado específicos da sua aplicação, têm de os inserir primeiro especificando o nome do atalho de teclado e depois conecta-lo. como os nossos atalhos estão todos presentes na barra de menu, temos de modificar os atalhos para as entradas de popup. Finalmente chamamos readSettings(), que lê as definições correntes a partir da janela principal do KDE contendo as configurações de atalhos standard, e depois as definições de atalhos específicos do ficheiro de configuração da aplicação. Quando nós aprofundarmos mais o nosso projecto de exemplo, iremos também falar sobre como configurar os atalhos específicos da nossa aplicação através de um diálogo de configuração, veja Configurar Barras de Menu e Ferramentas para essa parte do processo de desenvolvimento. O Modelo de Vista de Documento As duas próximas chamadas de funções membros, initDocument() e initView(), vão finalmente construir a parte que é suposto as janelas da aplicação disponibilizarem ao utilizador: um interface para trabalhar com os dados que é suposto a aplicação manipular; e essa é também a razão pela qual o esqueleto da aplicação contém três classes, uma classe *App, *View e *Doc. Para entender, porque esta estrutura é útil, iremos olhar um pouco à margem do código e introduzir alguma teoria, depois voltaremos ao programa de novo para ver como o esqueleto feito pelo KDevelop suporta esse modelo. Basicamente, tudo o que tem sido explicado até agora sobre a estrutura é que necessitamos de uma instância de aplicação que contenha a janela principal. Esta janela é responsável por disponibilizar o interface basico para o utilizador- contém as barras de menu, ferramentas ferramentas e estados e o controlo de eventos para a interacção do utilizador. Também, contém uma área, que é descrita como uma "vista". Agora, o objecto da visra é genericamente, mostrar os dados que o utilizador pode manipular, por ex. uma parte de um ficheiro de texto. Apesar do ficheiro de texto ser provavelmente maior que a vista é capaz de mostrar no ecrã, disponibiliza ao utilizador a possibilidade de ir para a parte que deseja ver (pelo que é uma vista), e aí o utilizador pode modificar os dados do conteudo do ficheiro. Para dar ao programador uma maneira melhor de separar as partes da aplicação através do código, o Modelo Vista-Documento foi inventado. Apesar de não ser um standard, disponibiliza uma estrutura de como uma aplicação deveria funcionar: A aplicação contém um objecto controlador, um objecto de Vista que mostra os dados com que o utilizador trabalha e um objecto Documento que contém na realidade os dados a serem manipulados. De regresso ao exemplo de trabalhar com um ficheiro de texto- aí, este modelo iria funcionar de forma a que o Documento leria o conteudo do ficheiro e disponibilizar os métodos para modificar os dados bem como para gravar o ficheiro de novo. A Vista então processa os eventos que o utilizador produz através do teclado e rato e utiliza os métodos do objecto Documento para manipular os dados do documento. Finalmente, o objecto controlador é responsável pela interacção do utilizador disponibilizando os objectos de Vista e de Documento bem como os interfaces para enviar comandos tais como abrir e gravar. Adicionalmente, certos métodos do objecto Vista podem ser disponibilizados por comandos que podem ser acedidos através de atalhos de teclado ou pelo rato ou barras de menu e ferramentas ferramentas. Este Modelo Vista Documento tem algumas vantagens- separa o código do programa de uma forma mais orientada por objectos e por isso oferece mais flexibilidade em geral, por ex. o mesmo objecto documento poderia ser apresentadps por duas vistas ao mesmo tempo; quer através de uma nova vista numa nova janela ou através de mosaico da janela corrente que contém dois objectos vista que constroem a região de vista da janela actual. Agora, se você vem dos sistemas MS-Windows poderá teralguma experiência com isso- o MFC já disponibiliza um modelo de documento que está pronto a utilizar. Para as aplicações KDE e Qt Qt, as coisas são ligeiramente diferentes. A Qt Qt é um poderoso conjunto de ferramentas pois disponibiliza as classes mais necessárias, widgets etc. Mas não existiu nenhuma intenção de tomar conta do modelo documento-vista, e como o KDE herda a Qt Qt, também não existiram nenhumas tendências de introduzir este modelo. Isto de certa forma tem a sua justificação no facto de que normalmente as aplicações X não funcionam com um MDI (Interface de Documento Multiplo). Cada janela principal é responsável pelos seus dados e isso reduz a necessidade de um modelo de documento pelo facto de que métodos para trabalhar em documentos são sempre implícitos em widgets. A única excepção a isto actualmente é o projecto KOffice que tenciona disponibilizar um conjunto de aplicações de produtividade completo tais como um processador de texto, uma folha de cálculo etc. Tecnicamente, isto é realizado com duas alterações à utilização normal da Qt Qt e KDE: o KOffice utiliza KOM e a implementação gratuita de CORBA chamada MICO para comunicação de objectos, as aplicações KOffice utilizam o modelo documento-vista para permitir a todas as aplicações trabalhar com quaisquer objectos de dados do KOffice Mas como o KDevelop actualmente se foca na utilização das actuasi bibliotecas do KDE 1.1.x e a Qt Qt 1.4x, nós não podemos utilizar este modelo por defeito- isto virá em distribuições futuras de um KDE 2, que irá (esperamos) conter duas modificações principais em relação à situação actual: um interface MDI para a KTMainWindowKTMainWindow as bibliotecas KOM que disponibilizam um modelo de documento Assim, a forma actual para os programadores de aplicações pode ser ou implementarem todos os métodos de documentos necessários dentro da sua vista ou tentarem reproduzir um modelo de documento por si. O KDevelop assim contém essa reprodução disponibilizando as classes necessárias e os métodos básicos que são geralmente utilizados para o Modelo Documento-Vista com a estrutura de aplicações para Qt Qt e KDE. De regresso ao código, você pode agora imaginar o propósito dos dois métodos que mencionamos no início desta secção: as funções initDocument() e initView(). O initDocument() constroi o objecto documento que representa a janela de dados da aplicação e inicializa atributos básicos tais como definir o bit de modificação que indica se os dados correntemente em utilização foram modificados pelo utilizador. Depois, o método initView() constroi o widget *View, conecta-o com o documento e chama o método setView() de KTMainWindowKTMainWindow para dizer à janela *App para utilizar o widget *View como a sua vista central. Para o programador, é importante saber que durante o processo de desenvolvimento ele tem de: re-implementar os métodos virtuais para eventos de rato e teclado disponibilizados pela QWidgetQWidget no objecto *View para disponibilizar meios de manipular os dados, re-implementar o paintEvent() da QWidgetQWidget no objecto *View para repaint() (re-desenhar) a cista após modificações, completar a implementação para imprimir o documento através do método de impressão do objecto *View, adicionar a serialização para o objecto *Doc para disponibilizar abertura e gravação de ficheiros, adicionar a implementação da estrutura de dados do documento ao objecto *Doc que está a representar os dados do documento logicamente em memória. adicionar quaiquer métodos que tenham de ser acessíveis pelo utilizador através de atalhos de teclado e menus/barras de ferramentas ferramentas. Configuração da Aplicação Agora, depois de termos criado todas as instâncias da instância KTMainWindowKTMainWindow da nossa aplicação para criar a primeira janela, nós temos de inicializar alguns valores que influenciam a aparência do programa. Para isto, chamamos readOptions(), que obtém todos os valores e chama os métodos necessários para definir os atributos correspondentes. A biblioteca KDE-Core contém a classe KConfigKConfig que disponibiliza uma boa possibilidade de guardar valores em ficheiros de configuração bem como de os ler novamente. Também, como cada instância KApplicationKApplication já cria o seu ficheiro de recursos, apenas temos de aceder a este ficheiro e criar os nossos valores. Como o KConfigKConfig nos disponibiliza o objecto de ficheiro, temos de utilizar a classe KConfigBaseKConfigBase para ler e escrever todas as entradas. Como escreve-lo é muito fácil de fazer com métodos writeEntry(), ler depende do tipo de atributo que queremos inicializar. Geralmente, uma entrada no ficheiro de configuração contém um nome de valor e o valor. Valores que pertençam conjuntamente num contexto podem ser coleccionados em grupos, pelo que temos de definir o nome do grupo antes de acedermos ao seu valor; o grupo tem de ser definido apenas uma vez para ler um conjunto de atributos que estão no mesmo grupo. Vamos observar o que queremos ler: 1 void KScribbleApp::readOptions() 2 { 3 4 config->setGroup("General Options"); 5 6 // bar status settings 7 bool bViewToolbar = config->readBoolEntry("Show Toolbar", true); 8 view_menu->setItemChecked(ID_VIEW_TOOLBAR, bViewToolbar); 9 if(!bViewToolbar) 10 enableToolBar(KToolBar::Hide); 11 12 bool bViewStatusbar = config->readBoolEntry("Show Statusbar", true); 13 view_menu->setItemChecked(ID_VIEW_STATUSBAR, bViewStatusbar); 14 if(!bViewStatusbar) 15 enableStatusBar(KStatusBar::Hide); 16 17 // bar position settings 18 KMenuBar::menuPosition menu_bar_pos; 19 menu_bar_pos=(KMenuBar::menuPosition)config->readNumEntry("MenuBar Position", KMenuBar::Top); 20 21 KToolBar::BarPosition tool_bar_pos; 22 tool_bar_pos=(KToolBar::BarPosition)config->readNumEntry("ToolBar Position", KToolBar::Top); 23 24 menuBar()->setMenuBarPos(menu_bar_pos); 25 toolBar()->setBarPos(tool_bar_pos); 26 27 // initialize the recent file list 28 recent_files.setAutoDelete(TRUE); 29 config->readListEntry("Recent Files",recent_files); 30 31 uint i; 32 for ( i =0 ; i < recent_files.count(); i++){ 33 recent_files_menu->insertItem(recent_files.at(i)); 34 } 35 36 QSize size=config->readSizeEntry("Geometry"); 37 if(!size.isEmpty()) 38 resize(size); 39 } Como vimos numa das partes de código acima, a primeira acção que o nosso construtor faz é: config=kapp->getConfig(); que define o ponteiro config tipp KConfigKConfig para a configuração da aplicação. Assim, não temos de nos preocupar com a localização do ficheiro de configuração. Na verdade, o ficheiro está, de acordo com o Standard de Sistema de Ficheiros KDE (KDE FSS), localizado em &$;HOME/.kde/share/config/; iremos observar mais atentamente o KDE FSS num passo futuro quando estivermos a definir localizações de ficheiros do projecto para instalação. Como o ficheiro de configuração é colocado no directório principal do utilizador, cada utilizador tem a sua aparência personalizada da aplicação excepto para valores que estão localizados num ficheiro de configuração para todo o sistema que pode ser criado opcionalmente e instalado pelo programador no directório do KDE. Mas, apesar de isto poder ajudar nalguns casos, devemos evitar quaiquer dependências da nossa aplicação no sentido da existência de entradas de ficheiro. Assim, todos os métodos de leitura disponibilizados pelo KConfigBaseKConfigBase permitem adicionar um valor de defeito a ser utilizado quando a entrada não existe. Outra coisa importante para um programador é que o ficheiro de configuração é guardado em texto simples, e isto é também por algumas razões pois tem de ter em atenção alguns critérios: o utilizador pode modificar o ficheiro de configuração através de um simples editor de texto se o utilizador quiser modificar valores manualmente, as entradas deverão ser muito transparentes para determinar o seu propósito para entradas que tenham de ser gravadas, mas são criticas em termos de segurança tal como chaves de acesso, você tem de procurar uma solução adequada para assegurar a segurança. Agora que sabemos os básicos, vamos analizar o código. Como dissemos, apenas temos de utilizar o nosso ponteiro config para aceder aos valores. Primeiro, na linha 4, nós definimos o grupo actual para "Opções Gerais". Isto indica que os valores estão de certo modo atributos gerais para a aplicação. Depois nós lemos os valores para a barra de ferramentas ferramentas e estados- estes têm de ser gravados quando a aplicação fecha para repor os seus estadps de novo quando o utilizador reiniciar o programa. Como as barras apenas podem estar ligadas ou desligadas, nós utilizamos um valor boolean, pelo que, o nosso método é readBoolEntry(). O processo é idêntico para ambas as barras, pelo que apenas temos de olhar para as linhas 7-10 para observar o que está a acontecer para a barra de ferramentas ferramentas. Primeiro, nós lemos o valor para uma variável temporária bViewToolbar na linha 7. O nome do valor no ficheiro é "Mostrar Barra de Ferramentas" e, se o valor não estiver presente (o que seria o caso na primeira vez que a aplicação iniciasse), o valor de defeito é definido para true (verdade). De seguida, nos definimos a marca de verificação da entrada de menu para (des)activar a barra de ferramentas ferramentas através deste valor: nós chamamos setItemChecked() no menu ver, entrada ID&_;VIEW&_;TOOLBAR com o nosso atributo. Finalmente, definimos a barra de ferramentas ferramentas para utilizar o valor. Por defeito, a barra de ferramentas ferramentas é visível, pelo que, apenas temos de fazer algo se bViewToolbar for false (falso). Com enableToolBar() (linha 10) estamos a definir a barra para se esconder automaticamente se for desactivada. De seguida, temos de ler as posições da barra. Como o utilizador pode ter modificado a posição da barra arrastando-a com o rato para outra área de cista, esta tem de ser gravada também e o seu estado reposto. Olhando para as classes KToolBarKToolBar e KMenuBarKMenuBar, vemos que as posições da barra podem ser: enum BarPosition {Top, Left, Bottom, Right, Floating, Flat} /* {Topo, Esquerda, Baixo, Direita, Flutuante, Fixa} */ Como este valor foi escrito num valor numérico, temos de o ler com readNumEntry() e converte-lo num valor de posição. Com setMenuBarPos() e setBarPos() dizemos às barrass ode aparecerem. Agora provavelmente notou que o nosso menu "Ficheiro" contém um menu para ficheiros utilizados recentemente. Os nomes de ficheiros são guardados numa lista de strings (conjunto de caracteres), que tem de ser gravado ao fecho da aplicação e agora tem de ser lido para repor o menu. Primeiro, inicializamos a lista com as entradas guardadas utilizando o readListEntry(). Depois, num ciclo for, criamos uma entrada de menu para cada item da lista. Finalmente, apenas temos de tomar conta da geometria da nossa janela. Nós lemos na aparência através de uma variável QSize contendo um valor x e y para o comprimento e altura da janela. Como a janela é inicializada através da KTMainWindowKTMainWindow, não temos de nos preocupar com o valor de defeito e apenas utilizamos resize() se a entrada não estiver vazia. O que resta explicar na construção da nossa aplicação é que inicialmente temos de desactivar comandos do utilizador disponíveis que o não podem estar no caso de algumas instâncias não corresponderem aos critérios necessários. Estes são gravação de ficheiros e operações que utilizem a área de transferência. Durante o tempo de vida da aplicação, temos de tomar conta destes várias vezes, mas isso é bastante fácil. A estrutura apenas nos dá dois métodos para (des)activar itens de barras de menu e ferramentas ferramentas com apenas uma chamada de método de cada vez. Executar Durante a secção anterior, apenas veriicaos o que acontecia durante a chamada de construção da nossa instância do KScribbleApp que nos disponibiliza a janela principal. Após regressar à função main(), temos de chamar show() para mostrar a janela. O que é diferente de qualquer KApplicationKApplication ou QApplicationQApplication aqui é que quando estamos a utilizar KTMainWindowKTMainWindow como a instância para o nosso widget principal, não temos de o definir com setMainWidget(). isto é feito pelo próprio KTMainWindowKTMainWindow e não temos de nos preocupar com isso. A única coisa que resta então é interpretar a linha de comandos. Obtemos a opção de linha de comandos e perguntamos, se int argc é > 1, o que indica que o utilizador chamou a nossa aplicação com kscribble filename&_;to&_;open. A nossa janela recebe então o pedido de abertura do ficheiro pelo se nome e chama openDocumentFile() com o nome do ficheiro. A última linha da função main() faz o trabalho conhecido: executa a instância da aplicação e o programa entra no ciclo de evento. Agora, na secção A Função main(), nós começamos a separar o processo de execução através de if( app.isRestored() ) e descrevemos o processo normal de invocação. De seguida daremos uma introdução à gestão de sessão e como a nossa aplicação a utiliza. Invocação por Gestão de Sessão Como dissemos, a função main() testa, se a aplicação foi invocada pelo gostor de sessão. O gestor de sessão é responsável por gravar o estado actual de todas as janelas de aplicação abertas no ambiente de trabalho do utilizador e tem de os repor quando o utilizador entrar da próxima vez, o que significa que a aplicação não é iniciada pelo utilizador mas invocada automaticamente. A parte do código executado foi: 6 if (app.isRestored()) 7 { 8 RESTORE(KScribbleApp); 9 } Em A Função main(), nós afirmamos que nós testamos a invocação perguntando à app.isRestored(). Depois a linha 8 é executada. Parece uma afirmação simples, mas de facto irá resultar num processo de execução complexo que queremos seguir nesta secção. A própria RESTORE() é uma macro disponibilizada pela KTMainWindowKTMainWindow. Expande-se para o seguinte código: if (app.isRestored()){ int n = 1; while (KTMainWindow::canBeRestored(n)){ (new KScribbleApp)->restore(n); n++; } } Isto irá repor todas as janelas de aplicação da classe KScribbleApp criando as instâncias e chamando restore() para a nova janela. É importante compreender que se a nossa aplicação utiliza vários widgets diferentes que herdam KTMainWindowKTMainWindow, você tem de expandir a macro e determinar o tipo de widgets de topo utilizando KTMainWindowKTMainWindow::classNameOfToplevel(n) em vez da classe KScribbleApp. O método restore() lê então a parte do ficheiro de sessão que contém a informação sobre a janela. Como a KTMainWindowKTMainWindow guarda tudo isto por nós, não temos de nos preocupar com mais nada. Apenas informação que pertence à nossa instância específica do KScribbleApp tem de ser então encontrada. Normalmente isto seria um ficheiro temporário que foi criado para guardar o documento ou outra initialização de que poderiamos necessitar. Para obter esta informação de reposição, nós apenas temos de passar por cima de dois métodos virtuais de KTMainWindowKTMainWindow, saveProperties() e readProperties(). A informação que temos de gravar no final da sessão é se o documento correntemente aberto está ou não modificado e o nome do ficheiro. Se o ficheiro estiver modificado, obteremos um nome de ficheiro temporário para o gravar. No início da sessão, esta informação é agora utilizada para repor o conteudo do documento: void KScribbleApp::readProperties(KConfig*) { QString filename = config->readEntry("filename",""); bool modified = config->readBoolEntry("modified",false); if( modified ){ bool b_canRecover; QString tempname = kapp->checkRecoverFile(filename,b_canRecover); if(b_canRecover){ doc->openDocument(tempname); doc->setModified(); QFileInfo info(filename); doc->pathName(info.absFilePath()); doc->title(info.fileName()); QFile::remove(tempname); } } else if(!filename.isEmpty()){ doc->openDocument(filename); } setCaption(kapp->appName()+": "+doc->getTitle()); } Aqui, a linha kapp->checkRecoverFile() parece um pouco estranha, pois o b&_;canRecover não está inicializado. Isto é feito pelo método que o define para true, se existir um ficheiro de reposição. Como apenas gravamos um documento num ficheiro de recuperação se foi modificado, definimos o bit de modificado directamente para indicar que a informação não foi gravada para o ficheiro correspondente. Também temos de ter em conta que o ficheiro de recuperação tem outro nome de ficheiro diferente do ficheiro original que foi aberto. Assim, temos de colocar o nome de ficheiro e caminho do antigo nome de ficheiro. Finalmente, temos a informação que queriamos recuperar e podemos apagar o ficheiro temporário através do gestor de sessão. Sumário: Durante este capítulo, ficou a saber como a aplicação se inicia quer por invocação normal do utilizador ou pelo gestor de sessão. Verificamos todo o código para aprender como as partes do interface visual da aplicação são construidas bem como inicializar os atributos através de entradas em ficheiros de configuração. Agora podemos executar a aplicação esqueleto para testar estas funções e ver como a janela do programa reage. Conteudos Adicionais de Projectos KDevelop Além do código fonte disponibilizado, os projectos KDevelop contêm imensas outras partes adicionais que são do interesse do programador. Estas são: um icon de programa um mini-icon de programa um ficheiro .kdelnk .kdelnk um ficheiro de documentação SGML SGML de exemplo um conjunto de documentação API API gerada a partir do código do esqueleto Excepto a documentação API API, estes elementos do projecto serão instalados juntamente com o binário da aplicação. Como o esqueleto do projecto tem de ser o mais aberto possível, tem de adaptar estas partes no sentido dos objectivos do seu projecto. Estas são primeiro editar os icons disponibilizados. Isto dará à sua aplicação um identificador único através do qual o utilizador pode determinar a sua aplicação visualmente em menus de gestão de janelas. O ficheiro .kdelnk .kdelnk é um ficheiro que instala a sua aplicação no kpanel no menu Aplicações. Este tem de ser editado definindo o caminho de instalação que será discutido mais tarde neste manual. Finalmente, a documentação que você irá disponibilizar ao utilizador está escrita em SGML SGML. Isto torna muito fácil criar várias apresentações diferentes para a mesma fonte. Por defeito, o KDevelop pode criar um conjunto de ficheiros HTML a partir desta fonte, para projectos KDE isto irá automaticamente utilizar o programa ksgml2html ksgml2html para adicionar uma aparência e utilização consistente tipo KDE à documentação. Numa secção posterior, iremos ver como a fonte SGML SGML é editada e o que temos de observar na instalação no utilizador final. Finalmente, a documentação API API (Interface de Programação da Aplicação) permite-lhe e a outros programadores entender rapidamente o código e utilizar as classes sem terem de adivinhar qual o objectivo de cada classe. Iremos aprender como expandir a documentação API API num passo posterior, por ora basta saber que a documentação é gerada pelo programa KDoc KDoc, que processa os ficheiros de cabeçalho e cria o resultado final em HTML, pelo que toda a documentação é colocada nos cabeçalhos. Desenho de Vista de Aplicação Quando se está a desenvolver uma aplicação com um interface gráfico de utilização, o trabalho principal é feito a disponibilizar a chamada "vista" para a aplicação. A vista geralmente é um widget que apresenta os dados de um documento e disponibiliza métodos para manipular o conteudo do documento. Isto pode ser feito pelo utilizador através de eventos que emite pelo teclado ou rato; operações mais complexas são muitas vezes processadas pelas barras de ferramentas ferramentas e menus que interagem com a vista e o documento. A barra de estados apresenta informação sobre o estado do documento, vista ou aplicação. Como exemplo, olhamos para como um editor está construido e onde podemos encontrar cada parte. Um editor é suposto geralmente disponibilizar um interface para o utilizador ver e/ou modificar o conteudo de um documento de texto. Se você iniciar o KEdit, vê o interface visual da seguinte forma: A barra de menu: disponibiliza operações complexas bem como abrir, gravar e fechar ficheiros e sair da aplicação. A barra de ferramentas ferramentas: oferece icons que permitem um acesso rápido para as funções mais necessárias, A barra de estados: apresenta o estado da posição do cursor através da linha e coluna actuais, A vista no centro da janela, apresentando um documento e oferecendo um cursor conectado ao teclado e rato para trabalhar sobre os dados. Agora é fácil compreender, que uma vista é uma parte única da aplicação e que o desenho da vista decide sobre a usabilidade e aceitação de uma aplicação. Isto significa que um dos primeiros passos no desenvolvimento é determinar o objectivo da aplicação e que tipo de desenho lhe iria mais de encontro para permitir a qualquer utilizador trabalhar com a aplicação com um mínimo de trabalho de aprendizagem de como utilizar o interface gráfico. Para alguns objectivos como edição de texto e apresentação de ficheiros HTML, as vistas são disponibilizadas pelas bibliotecas Qt Qt e KDE KDE; iremos discutir alguns aspectos destes widgets de alto-nível na próxima secção. Mas para a maioria das aplicações novos widgets têm de ser desenhados e implementados. É isso que faz um programador e também um desenhador e onde as suas capacidades em criatividade são necessárias. De qualquer modo, deverá procurar a intuição primeiro. Lembre-se que imensos utilizadores não aceitarão uma aplicação que não seja bonita graficamente, oferece imensas funcionalidades, fácil de utilizar, rápida de aprender como utilizar. Desnecessário dizer que a estabilidade é um dos objectivos principais. Ninguém pode impedir erros de código, mas um mínimo pode ser atingido pelo menos por objectivos de desenho inteligentes e a utilização de desenho orientado por objectos. C++ torna a programação um prazer se você souber como explorar as suas capacidades- hereditariedade, ocultação de informação e reutilização de código já existente. Quando se cria um projecto KDE ou Qt Qt, você tem sempre de ter uma vista que herda QWidgetQWidget, quer por herança directa ou porque a biblioteca do widget que quer utilizar herda QWidgetQWidget. Assim, o Assistente de Aplicação já constroi a vista que é uma instância da classe &<;yourapp&>;View, que já herda QWidgetQWidget. A aplicação cria a sua vista no método initView(), onde uma instãncia é criada e conectada com o widget principal como sua vista com KTMainWidget::setView(). Este capítulo descreve assim como utilizar bibliotecas de widgets para criar vistas de aplicações KDE ou Qt Qt que são geradas com o KDevelop, depois iremos observar as bibliotecas e que tipos de vistas são já oferecidas. Utilizar Bibliotecas de Vistas Quando o desenho da sua aplicação estiver definido, você deverá primeiro procurar código já existente que fará a sua vida muito mais simples. Uma parte desta busca é a procura de um widget que possa ser utilizado como vista ou pelo menos parte dela; quer directamente ou por hereditariedade. As bibliotecas KDE e Qt Qt já contêm um conjunto de widgets que podem ser utilizados para este propósito. Para os utilizar, você tem duas opções: remover a nova classe de cista e criar uma instância de um widget de biblioteca; depois defini-lo como vista, modificar a hereditariedade da classe de vista disponibilizada para a classe do widget de biblioteca a utilizar. De qualquer uma das formas, é importante saber que se a estrutura de aplicação não está actualmente ligada a nenhuma biblioteca que contenha o widget, o linker irá falhar. Depois de se decidir a utilizar um certo widget, procure a biblioteca a linkar; depois abra "Projecto"->"Opções" na barra de menu do KDevelop. Vá para a página "Opções do Linker" e procure as caixas de selecção que indiquem as bibliotecas que estão correntemente a serem utilizadas. Se a biblioteca do seu widget de vista já estiver seleccionada, pode deixar as opções do projecto intocadas e começar a fazer as alterações necessárias devido à sua escolha. Caso contrário, a opção de linker permite adicionar a biblioteca através de uma caixa de selecção, active-a e prima "OK" para sair de novo do diálogo de opções do projecto. Em qualquer outro caso, adicione a biblioteca na linha de edição por baixo da opção -l. Para bibliotecas que a sua aplicação tem de procurar antes de preparar os ficheiros Makefiles através do script configure na máquina do utilizador final, adicione a seguinte macro de procura ao ficheiro configure.in localizado na raiz do directório do seu projecto e adicione a macro à linha de edição. Lembre-se que tem de correr "Construir"->"Autoconf e automake" e "Construir"->"Configurar" antes dos Makefiles conterem as expansões correctas para a macro de biblioteca. Também, se os ficheiros inclued para a biblioteca a adicionar não estiverem no caminho actual de include (que pode ser visto pela opção -I na janela de output ao fazer "Make"), tem de adicionar o caminho no diálogo de Opções de Projecto -página "Opções de Compilador" com a opção -I ou a correspondente macro de automake na linha de edição para "Opções Adicionais". Qt <indexterm remap="idx"><primary>Qt</primary></indexterm> Views Looking at the first page of the Qt Qt online documentation, you will find a link to "Widget Screenshots" where you can have a look at how the widgets Qt Qt contains look like. These are ready to use and can be combined together to form complex widgets to create application views or dialogs. In the following, we'll discuss some of these which are very usable for creating application views, but keep in mind that the KDE libraries KDE libraries sometimes contain other widgets for the same purpose; those will be reviewed in the next section. Here are a set of hints for what purpose you could use which Qt Qt component: if your view area isn't big enough to display all your data, the user must be enabled to scroll over the document with bars on the left and bottom of the view. For this, Qt Qt provides the class QScrollView, which offers a scrollable child area. As explained, you could inherit your own widget from QScrollView or use an instance to manage your document's view widget. to create a ScrollView yourself, inherit the View widget from QWidgetQWidget and add vertical and horizontal QScrollBars. (this is done by KDE`s KHTMLView widget). for text processing, use QMultiLineEdit. This class provides a complete text editor widget that is already capable to cut, copy and paste text and is managed by a scrollview. use QTableView to display data that is arranged in a table. As QTableView is managed by scrollbars as well, it offers a good solution for table calculation applications. to display two different widgets or two widget instances at the same time, use QSplitter. This allows to tile views by horizontal or vertical dividers. Netscape's Mail window is a good example how this would look like- the main view is separated by a splitter vertically, the right window then is divided again horizontally. QListView displays information in a list and tree. This is useful for creating file trees or any other hierarchical information you want to interact with. You see that Qt Qt alone offers a whole set of widgets which are ready to use so you don't have to invent new solutions if these match your needs. The sideffect when using standard widgets is that users already know how to handle them and only have to concentrate on the displayed data. KDE Views The KDE libraries KDE libraries were invented to make designing applications for the K Desktop Environment easier and capable of more functionality than what Qt Qt alone is offering. To see what's available, we have a look at the documentation tree in KDevelop. You see that the KDE libraries start with kdecore, which is a base for all KDE applications KDE applications. Then,kdeui offers user interface elements. This is where we will find some useful things first. For creating new applications, the kdeui library offers: KTabListBoxKTabListBox: offers a multi-column list box where the user can change the rows with drag'n drop drag'n drop. KTreeListKTreeList: inherited from QTableViewQTableView, offering a collapsible tree. This could be used instead of QListView. KEditKEdit: the base classes for the KEdit application offered with KDE. This could be used instead of QMultiLineEditQMultiLineEdit. KNewPannerKNewPanner: manage two child widgets like QSplitterQSplitter. The khtmlw library on the other hand offers a complete HTML-interpreting widget that is ready to use. It is scrollable already, so you don't even have to take care for that. A possible use could be to integrate it as a preview widget for an HTML editor; used by applications such as KFM, KDEHelp KDEHelp and KDevelop to display HTML files. Creating your own Views Now that you have a general overview of what is already provided, you may notice that for a lot of purposes already existing widgets can be used or combined together. KMail is an example as well as KDevelop itself makes use of library view components to display data. For applications that use a special file format or have to deal with graphics, you are probably forced to create your own view widget to allow data manipulation. This is realized in our sample by the class KScribbleView, already providing a base for a view area. The inheritance from QWidgetQWidget is necessary to overwrite the virtual methods to process user events, this is probably the most work besides providing popup menus for easier access of certain functions. Also it is likely that you have to implement a set of slots which can be accessed by toolbar toolbar buttons or menu bar commands to connect to as well as methods to manipulate variables such as e.g. a painter color. For completeness, we will repeat the necessary methods: a) Keyboard events --TAB and Shift-TAB keys: changes the keyboard input focus from the current widget to the next widget in the focus order. The focus can be set to widgets by calling setFocusPolicysetFocusPolicy()() and process the following event handlers: virtual void focusInEventfocusInEvent() ( QFocusEventQFocusEvent * ) virtual void focusOutEventfocusOutEvent() ( QFocusEventQFocusEvent * ) b) all other keyboard input: virtual void keyPressEventkeyPressEvent() ( QKeyEventQKeyEvent * ) virtual void keyReleaseEventkeyReleaseEvent() ( QKeyEventQKeyEvent * ) c) mouse movements: virtual void mouseMoveEvent ( QMouseEventQMouseEvent * ) virtual void enterEvententerEvent() ( QEventQEvent * ) virtual void leaveEventleaveEvent() ( QEventQEvent * ) d) mouse button actions: virtual void mousePressEventmousePressEvent() ( QMouseEventQMouseEvent * ) virtual void mouseReleaseEventmouseReleaseEvent() ( QMouseEventQMouseEvent * ) virtual void mouseDoubleClickEventmouseDoubleClickEvent() ( QMouseEventQMouseEvent * ) e) window events containing the widget: virtual void moveEventmoveEvent() ( QMoveEventQMoveEvent * ) virtual void resizeEventresizeEvent() ( QResizeEventQResizeEvent * ) virtual void closeEventcloseEvent() ( QCloseEventQCloseEvent * ) When re-implementing these functions, you should watch certain issues to avoid implementation mistakes that will make it almost impossible to change the widget's behavior afterwards: declare your virtual methods as virtual as well and keep the access to protected. This allows code-reuse by inheritance and is consistent. don't hard-code any event-processing which should be made configurable. This counts most for keyboard events which should be realized with keyboard acceleratoraccelerators if any function is called. This even counts for text processing ! (Imagine that a lot of users are familiar with their favorite editor's behavior. If this is configurable, they can use the behavior they like and are used to) forward popup menu highlighting signals to the main widget to enable statusbar help Configuring Menubars and Toolbars Menubars and toolbar toolbars are one of the most important parts of an application to provide methods to work with a document structure. As a general rule, you should make all functions available by the menubar. Those methods that should not be available at a current stage of the application process should be disabled. Further, an application can only contain one menubar, but several toolbar toolbars. Toolbars on the other hand should contain only the most frequently used commands by pixmap icons or provide quick access methods like combos to select values. How does it work ? Each entry, may it be a menuentry or a toolbar toolbar item, has a resource ID which is an integer value. As these values can't be used twice, those are set by macros, where the numeric values are replaced by a descriptive ID name that can be used in your sources then. All resource ID's are collected in the file resource.h, where you can keep an overview over the used values. Anyway, the compiler will inform you if you've used a value twice for constructing entries. Also, the resource file should contain all menu acceleratoraccelerators by IDK macro replacements. An example: (resource.h) #define ID_VIEW_TOOLBAR 12010 (kscribble.cpp) // menu entry Toolbar in the "view" menubar menu view_menu->insertItem(i18n("&&;Toolbar"), ID_VIEW_TOOLBAR); This inserts the entry Toolbar to the View popup menu of the menubar in the kscribble application. The resource ID's name is held to contain the menu name and the action's name visible. The ampersand is set in front of the letter that functions as a keyboard acceleratoraccelerator and the entry itself is embraced by the i18n() internationalization macro. On activating the menu item, the commandCallback() switch is called with the ID number. There, you have to add an according comparator value with the method you want to execute on activating the menuentry: case ID_VIEW_TOOLBAR: slotViewToolBar(); break; Note: you don't have to use the ID system. If no ID is given, the menu gets numbered automatically. The KDevelop framework uses this as it allows accessing menu and toolbar toolbar ID's to create switch statements that select the slot to call on activated() for menus, clicked() for toolbar toolbar buttons. The connection can also be made directly using the provided methods of the classes providing menus and toolbar toolbars. Adding a new menu A new menubar is added to an application by the following: add a pointer to the new menu in the App-class header call the constructor of QPopupMenuQPopupMenu to the pointer in initMenuBar() at the location where your menubar should appear later. insert the according menu-items into the popup menu and set their resource ID's in the resource file add connects for commandCallback() and statusCallback() to the menu at the end of initMenuBar() add the methods you want to call by the menu-entries in the header and implementation file. add the switch statements for the entries to the commandCallback() and statusCallback() methods Integrating Toolbar buttons Toolbar buttons can be added like menu-entries with the difference that the used method is insertButton() and takes a button pixmap and tool-tip text instead of a menu text. The icons you want to use can be loaded by KIconLoader, where KApplicationKApplication offers the macros ICON() and Icon() to access the icon loader and load the icon. These macros take the filename of the pixmap as their parameter to load the icon from the KDE file system in a certain order (see KIconLoader for the search order). The KDE libraries KDE libraries also offer a set of toolbar toolbar buttons that can be used for standard actions. In cases where they don't meet your needs, you will have to paint your own pixmaps. KDevelop supports this by selecting "New" from the "File" menu, then select Pixmap as the file type. Usually you will place your toolbar toolbar pixmaps in a project subdirectory "toolbar toolbar" and install them into your application specific toolbar toolbar directory. Configuring Statusbars The KDevelop projects already make use of the statusbar by providing statusbar messages for menu-entries and toolbar toolbar buttons. When adding a menuentry, also add your status message in the method statusCallback(). statusCallback() uses the method slotStatusHelpMsg() to display a statusbar message for two seconds. When executing a command, you should use the method slotStatusMsg() at the beginning with the string describing what your application does; before a return or method end, you should reset the statusbar message with a "Ready." string calling the same method. Keyboard Accelerator Configuration A very professional thing you should always add to your application are keyboard acceleratoraccelerators. Those are mainly used by experienced users that want to work fast with their applications and are willing to learn shortcutshortcutss. For this, the KDE libraries KDE libraries provide the class KAccelKAccel, which provides the keyboard acceleratoraccelerator keys and access to global configured standard keyboard acceleratoraccelerators. By default, frame applications generated by KDevelop only use standard keyboard acceleratoraccelerators such as F1 for accessing online-help, Ctrl+N for New File etc. You should look for the keyboard acceleratoraccelerators already set in KAccelKAccel first before adding a new acceleratoraccelerator. If your application contains a lot of acceleratoraccelerators, you should make them configurable by an Options-menu; either it could be combined with other application configuration in a QWidgetQWidget or stand alone. The KDE library already provides a KKeyChooser for use in tab dialogs, whereas KKeyDialog provides a ready-to use key-configuration dialog. See the following classes for more information: KAccelKAccel(kdecorekdecore), KKeyChooserKKeyChooser, KKeyDialogKKeyDialog (kdeuikdeui) The Dialogeditor: Where your Dialogs are Build What the Dialogeditor provides The built-In dialogeditor of KDevelop is designed to help you construct widgets and dialogs that fit your application's purpose and reduces the time rapidly to extend the GUI of your application. The only limitation for now is that the dialogeditor does not support geometry management that is provided by Qt Qt; therefore the dialogs are static in their size and this may lead to certain circumstances where e.g. the label width is not long enough to support the full length of a translation. On the other hand, the current state of the editor in conjunction with KDevelop's project management offers the fastest way to create full-featured applications for the K Desktop Environment. Qt <indexterm remap="idx"><primary>Qt</primary></indexterm> and KDE Widgets Currently provided widgets are: QT-Widgets: QWidgetQWidget - a widget that can be specified by yourself and can contain other widgets as well. This allows creating a widget hierarchy within your dialog. QLabel QLabel - a label that represents text information on the widget. Use QLabel QLabel e.g. in front of lineedits to signal what the purpose of the line-edit is or which variable e.g. a combo box allows to set. QPushButton - a button that allows to e.g. call another dialog like QFileDialog for selecting a filename. QCheckBox - a check box for e.g. enabling/disabling options. QCheckBox is widely used for configuration dialogs. QLCDNumber - displays numbers in LCD style. Often used for clocks. QRadioButton - like QCheckBox often used to let the user set any options. QRadioButton specializes the options setting when more of them depend on each other, e.g. you have three radio-buttons, but you want the user to choose one of three offered options. See QButtonGroupQButtonGroup for additional information. QComboBoxQComboBox - a combo box lets the user set a value by selecting it from a drop-down menu or by inserting the value, if the box is write enabled. QListBox - provides a single-column list of items that can be scrolled. QListView - creates a multi-column list view that can be used to display e.g. file trees etc. in tree and table view. QMultiLineEdit - offers a multi-line editor. QProgressBar - displays the progress of an action that takes a longer time to be finished. QSpinBox - allows choosing numeric values by up- and down buttons or insertion if write enabled. QSlider - sets a value in a program-defined range by a slider. QScrollBar - indicates the range of a value and sets the current value by a slider as well as up- and down buttons; often used for widgets whose contents is larger than the actually visible view area. By using the scrollbar, the visible area can be changed to another part of the widgets' contents. QGroupBox - provides a group box frame with title to indicate that child widgets within the box belong together. KDE-Widgets: KColorButton - a pushbutton displaying a selected color. On a button press, the KDE Color dialog is shown where the user can select another color. Often used for drawing applications or in any case where color values can be set. KCombo - similar to QComboBoxQComboBox. Lets the user choose a value by a drop-down list box. KDatePicker - a complete widget to get a date value by the user. KDateTable - a calendar table to select a date of a month. Used by KDatePicker to build the date picker dialog. KKeyButton - a button to select a key value. If the button is selected, it gets activated. Pressing a keyboard button will change the key value for the button which can be used to configure key-bindings. KLed - and LED (Light Emitting Diode) widget to display a certain state. KLedLamp - and LED lamp that also supports click actions KProgress - similar to QProgressBar, KProgress supports certain other values. KRestrictedLine - a QLineEdit that only accepts certain user input. This can be used to restrict access to certain data by password dialogs. KSeparator - a separator widget to be used in all cases where KDE applications KDE applications require a separator to provide a unique look. Often used in dialogs to separate logical parts where QGroupBox doesn't fit. KTreeList - a collapsible list view to display trees similar to QListView. Properties of Qt <indexterm remap="idx"><primary>Qt</primary></indexterm> supported Widgets The following chapter gives a complete overview over the currently supported widgets of the Qt Qt toolkit. To achieve a better understanding of the properties, these are separated to their inheritance. As all of them inherit QWidgetQWidget, this class is described first. All QWidgetQWidget properties are available for all other widgets as well, so these are not listed for them again. For widget groups that inherit an abstract subclass of QWidgetQWidget as their base-class, the base-classes' properties are listed first (though this class does not provide a widget in the dialogeditor itself). Then the widget properties for the available widget of the group contains the properties that are specific to it. For a better understanding the inheritance tree of the available widgets is listed below: <cdx/QWidget/ <cdx/QButton/ (abstract) QCheckBox QPushButton QRadioButton <cdx/QComboBox/ QFrame (abstract for now) QGroupBox QLCDNumber <idx/QLabel/ QProgressBar QScrollView (abstract for now) QListView QSpinBox QTableView (abstract) QListBox QMultiLineEdit QLineEdit QScrollBar QSlider <indexterm remap="cdx"><primary><literal>QWidget</literal></primary></indexterm>QWidget Properties QWidgetQWidget is the base class for almost all widgets in Qt Qt and KDE. Therefore widgets that inherit QWidgetQWidget will allow to use the same settings in most cases. Appearance: BgColor: Background color of the widget BgMode: Background mode of the widget BgPalColor: Color pallette for the background BgPixmap: filename for a background pixmap Cursor: Cursor over the widget Font: Font for the widget MaskBitmap: filename for a masking bitmap. C++ Code: AcceptsDrops: if set to true, the widget item will accept drops by drag'n drop drag'n drop mechanisms (Qt Qt drag'n drop drag'n drop protocol, not KDE 1.x !) Connections: connects the item's signals to slots FocusProxy: the item that gives its focus to this widget. HasFocus: sets if the item has the focus by default. Mind that only one item per dialog can have this value as true ResizeToParent: resizes the widget to its parent's size (not visible in editing mode) VarName: Variable name of the item. Change this to names that describe the item's purpose. General: IsEnabled: sets if the widget will accept user events IsHidden: sets the item to be visible(false) or hidden(true) Name: sets the name of the widget. Mind that the name is different from the VarName in C++ Code. Geometry: Height: height of the item IsFixedSize: MaxHeight: maximum value for Height MaxWidth: maximum value for Width MinHeight: minimum value for Height MinWidth: minimum value for Width SizeIncX: pixel steps for resize actions to X direction SizeIncY: pixel steps for resize actions to Y direction Width: width of the item X: position horizontal, counted from the left corner Y: position vertical, counted up to down <indexterm remap="cdx"><primary><literal>QButton</literal></primary></indexterm>QButton inherited widgets QButtonQButton is an abstract widget class that provides properties common to buttons. Inherits <cdx/QWidget/ Inherited by QCheckBox, QPushButton and QRadioButton inherit QButtonQButton. Appearance: setPixmap: sets the pixmap filename to use General: setText: the text on labels, buttons and boxes, also pre-set text for lineedits. setAutoRepeat: if enabled, the clicked() signal is emitted at regular intervals while the button is down. No effect on toggle buttons. setAutoResize: Enables auto-resizing if TRUE. When auto-resize is enabled, the button will resizes itself whenever the contents changes. QCheckBox Properties Inherits <cdx/QWidget/ and <cdx/QButton/ General: isChecked: (setChecked) defines is the checkbox is set checked on construction QPushButton Properties Inherits <cdx/QWidget/ and <cdx/QButton/ General: isAutoDefault: (setAutoDefault) the auto-default button becomes the default push button if it receives the keyboard input focus. isDefault: (setDefault) there can be only one default button and it is only allowed to use in a dialog (see QDialog). The default button emits clicked() if the user presses the Enter key. isMenuButton: (setIsMenuButton) tells the button to draw a menu indication triangle if enabled. The menu has to be inserted separately. isToggleButton::(setToggleButton) makes a push button a toggle button, so the button has a similar state as check boxes. isToggledOn: (setOn) (public slot) switches a toggle button on. QRadioButton Properties Inherits <cdx/QWidget/ and <cdx/QButton/ General: isChecked: (setChecked) defines is the radio button is set checked on construction <indexterm remap="cdx"><primary><literal>QComboBox</literal></primary></indexterm>QComboBox Properties Inherits <cdx/QWidget/ ( no additional properties for now) QFrame inherited widgets Inherits <cdx/QWidget/ For now only used as an abstract class. Appearance: Margin (setMargin): sets the margin, which is the distance from the innermost pixel of the frame and the outermost pixel of the contents. QGroupBox Properties Inherits <cdx/QWidget/ and QFrame General: Title: (setTitle) sets the group box title that is displayed in the box frame. QLCDNumber Properties Inherits <cdx/QWidget/ and QFrame General: NumDigits:(setNumDigits) sets the number of digits displayed in QLCDNumber Value: (display) (public slot) sets the initial value for QLCDNumber QLabel <indexterm remap="idx"><primary>QLabel</primary></indexterm> Properties Inherits <cdx/QWidget/ and QFrame Appearance: Margin (setMargin): sets the margin, which is for QLabel QLabel the distance from the frame to the first letter of the label text, depending on the alignment of the label. C++ Code: Buddy: (setBuddy) sets the buddy widget of the label. General: Text: (setText) sets the label text. isAutoResize: (setAutoResize) if TRUE, the label will resize itself if the contents changes. The top left corner is not moved. QProgressBar Properties Inherits <cdx/QWidget/ and QFrame General: TotalSteps: (setTotalSteps) (public slot) sets the total steps of the progress bar. During the iteration of your action to display the progress, you have to call setProgress(int) to advance the progress step displayed to (int). QScrollView Inherits <cdx/QWidget/ and QFrame Inherited by QListBox (abstract for now) Provides a scrollable widget that manages the display of a child widget by a vertical and horizontal scrollbar. QListView Inherits <cdx/QWidget/, QFrame and QListView Provides a list view to display hierarchical data either in a table or a tree. Manages itself by scrollbars through QScrollView. Appearance: ListViewFont: (setFont()) Sets the font of the ListView items ListViewPalette: (setPalette()) Sets the palette of the list view items TreeStepSize: (setTreeStepSize(int)) Offset of pixels of a child item to its parent item hScrollBarMode: Scrollbar mode provided by QScrollView for the horizontal scrollbar isAllColumnsShowFocus: (setAllColumnsShowFocus(bool)) displays focus on all columns of an item. isMultiSelection: enables multi-selection of list items isRootDecorated: enables the + and - decoration to open and close trees vScrollBarMode:Scrollbar mode provided by QScrollView for the vertical scrollbar General: Entries: lets you insert a list of entries that are pre-set as QListViewItems. isAutoUpdate: QSpinBox Properties Inherits <cdx/QWidget/ and QFrame General: MaxValue: the maximum value the user can choose MinValue: the minimum value the user can choose Prefix: Suffix: Value: the pre-set value when the widget is shown isWrapping: QTableView inherited widgets Inherits <cdx/QWidget/, QFrame and QTableView Inherited by QListBox and QMultiLineEdit QListBox Properties Inherits <cdx/QWidget/, QFrame and QTableView General: isAutoBottomScrollBar: (setAutoBottomScrollBar) isAutoScroll: (setAutoScroll) isAutoScrollBar: (setAutoScrollBar) isAutoUpdate: (setAutoUpdate) isBottomScrollBar: (setBottomScrollBar) isDragSelect: (setDragSelect) isSmoothScrolling: (setSmoothScrolling) Geometry: setFixedVisibleLines: sets a fixed height for the widget so that the given number of text lines are displayed using the current font. QMultiLineEdit Properties Inherits <cdx/QWidget/, QFrame and QTableView General: Text: (setText) (public slot) sets the text of the widget. isAutoUpdate: (setAutoUpdate) used to avoid flicker during large changes; the view is not updated if disabled. isOverWriteMode: (setOverwriteMode) (public slot) sets overwrite enabled or disabled. isReadOnly: (setReadOnly) (public slot) sets the widget text to read only; disables text input. isTextSelected: (selectAll)(public slot) marks the whole text selected Geometry: setFixedVisibleLines: sets a fixed height for the widget so that the given number of text lines are displayed using the current font. QLineEdit Properties Inherits <cdx/QWidget/ General: CursorPosition: (setCursorPosition) sets the default cursor position. MaxLength: (setMaxLength) sets the maximum string length Text: (setText) (public slot) sets the contents displayed on construction hasFrame: (setFrame) draws the line edit within a two-pixel frame if enabled. isTextSelected: (selectAll) (public slot) sets the text to be selected. QScrollBar Properties Inherits <cdx/QWidget/ and QRangeControl. General: MaxValue: sets the maximum slider value; used in constructor (optional) MinValue: sets the minimum slider value; used in constructor (optional) Orientation: (setOrientation) sets the orientation of the scrollbar to horizontal or vertical. Value: sets the initial value of the scrollbar in the constructor (optional) isTracking: (setTracking) if enabled, the scrollbar emits valueChanged() whenever the bar is dragged; otherwise only on mouse release. QSlider Properties Inherits <cdx/QWidget/ and QRangeControl. General: MaxValue: sets the maximum slider value; used in constructor (optional) MinValue: sets the minimum slider value; used in constructor (optional) Orientation: (setOrientation) sets the orientation of the slider to horizontal or vertical. Value: (setValue) (public slot) uses QRangeControl::setValue() to set the value. isTracking:(setTracking) if enabled, the slider emits valueChanged() whenever the slider is dragged; otherwise only on mouse release. Properties of KDE supported Widgets <cdx/QWidget/ <cdx/QButton/ (abstract) QPushButton KColorButton KKeyButton <cdx/QComboBox/ KCombo QFrame (abstract for now) KDatePicker KLedLamp KProgress KSeparator QTableView (abstract) KDateTable KTreeList QLineEdit KRestrictedLine KLed KColorButton Inherits QPushButton General DisplayedColor (setColor()) the displayed color on the button KKeyButton KCombo Inherits: <cdx/QComboBox/ General Entries the string list of entries displayed in the combo box Text the text displayed in the combo box currently isAutoResize resizes the combo box to the current item KDatePicker Appearance FontSize the font size for the date picker KLedLamp KProgress KSeparator General Orientation sets the orientation of the separator to horizontal or vertical; default is horizontal KDateTable KTreeList Appearance TreeListBgColor TreeListPalette isBottomScrollbar isScrollBar isShowItemText isSmoothScrolling isTreeDrawing General Entries isAutoUpdate KRestrictedLine KLed Inherits <cdx/QWidget/ Appearance LedColor: (setColor()) sets the displayed LED color Constructing a new Dialog Constructing a new dialog is very easy if you already have experience with graphical construction applications. KDevelop offers to create a widget visually and displays the look as it will be shown to the user directly. Further, you can have a preview of your widget by selecting "Preview" from the "View" menu. To begin constructing a dialog or any other widget, switch to the Dialogeditor and select "New Dialog" from the "File" menu. Then enter all needed information to the "New Dialog" dialog. Those are: The Dialog inheritance. This is necessary because any widget is at least derived from QWidgetQWidget. Besides the widget types provided by Qt Qt, you can inherit e.g. form an abstract base class you constructed yourself within your project. In this case, select "custom" and enter the header file path to the line edit below. The Dialog name. This sets the class name of the generated dialog. Select a classname that is descriptive for what the dialog does; in cases of inheritance from QDialog, you may enter a name that ends with Dlg to remember yourself it's a dialog. Naming convention should match that of KDE and Qt Qt: Use uppercase letters for your classname. For e.g. a grid-size selection dialog, you would enter GridSizeDlg. The generated filenames. Those are preset when entering the dialog name, but can be changed afterwards. If you want to use other filenames, the naming convention should also match that of KDE and Qt Qt: the filenames are all lowercase and contain the classname to remember what class is kept where. The data file that has to be set will later contain the generated code that will build up your dialog. You should not edit this file manually afterwards; use the implementation file for any additions towards dialog construction code and method implementations. The dialog will then show itself as a widget with a grid. As the dialogeditor uses the grid to snap any child widgets to the geometry, you can change the grid size with the "Grid Size" entry in the "View" menu, if the preset values don't match your needs. Then select the "Widgets" tabulator on the left pane and press the button for the widget item you want to add to the main widget. It directly appears on the main widget's left upper corner and gets selected by a resizable frame. Then move or resize the widget with the mouse. The cursor will change to indicate which action can be done at the current position. After having finished the construction, select "Generate Files" from the "Build" menu or hit the according toolbar toolbar button. The files will then be generated at the preset location and included into your project sources. A rebuild or make will compile all generated files within your project and you can add the according constructor call to the application to invoke the dialog or widget. For KDE projects, all widget properties that will be visible later, e.g. label texts, are set with the i18n() macro of kapp.hkapp.h to support internationalization. Therefore you should do a "Make Messages and merge" when finished with construction and implementation. When creating a dialog or widget, you should watch the following guidelines: Always try to be consistent! This is probably the most important rule when constructing GUI elements. Mind that the user will only accept an application that is easy to understand no matter how complex it's purpose may be. Add help wherever you can by tool-tips, What's this..? help or Quick-help. This allows getting direct information about the purpose of the GUI elements. watch the keyboard focus keyboard focus ! The generator does not take care of that- this has to be watched when constructing any widget; otherwise you have to reorder your initialization code by hand which is a very unthankful job. The keyboard focus keyboard focus on any widget means the oder on which items get the keyboard input focus when the user presses the tab and shift+tab button. It would be very annoying if the focus changes everywhere but not to the next widget visible below or to the right of the current widget. Therefore start constructing your widget top down from left to right to ensure the consistency of the focus. Setting Widget Properties widget propertiesdialogeditorproperties window Widget properties can be set easily with the properties window entries. When a widget gets selected, the properties window automatically updates to the properties of the current widget. As all widgets are derived from QWidgetQWidget, you can set the QWidgetQWidget properties plus an amount of properties that are specific to the selected widget. Properties can be: Integer values, such as the geometry of a widget or the font size Boolean values to enable/disable certain parameters of the widget. Set with combos containing true and false enumerable values of a widget, e.g. the palette. Set with combos containing all possible values Color values for e.g. the displayed color. Set with the KDE Color Dialog Font values for e.g. labels. Be careful to set Font values other than the default because this may prevent KDE from updating the font. Set with the KDE Font Dialog File names for e.g. background pixmaps. Do not use gif images here as these may get unsupported by further Qt Qt versions > 1.42 Integrating the Dialog Whenever you created a widget, you probably want to add it to the project to execute the action it is designed for. As a widget can be constructed for several purposes, we will watch for two cases: a QWidgetQWidget inherited widget and a QDialog one. <literal remap="tt"><indexterm remap="cdx"><primary><literal>QWidget</literal></primary></indexterm>QWidget</literal> inherited Let's say you created a widget that will be part of the main view. If it fills the whole view area, you have to add an instance pointer to the header declaration of your KTMainWindowKTMainWindow instance replacing the currently set view widget. Then change the code in the initView method to set this widget the main view. Additionally, you could remove the View class of the generated project, but mind that the document instance and the App instance depends on the view class. In this case, it is technically a much better way to create a mini-KDE application and construct your KTMainWindowKTMainWindow instance yourself. More often the widget is intended to be a part of the view area, which means it is combined with other widgets. This can be done by using one of the following classes that provide a divider to separate two widgets: QSplitter KPanner KNewPanner If the main view shall contain more than two widgets, you have to use another instance of the divider as one of the two managed widgets by the first one. Then add the according widgets to each panner and set the first panner the view area. <literal remap="tt">QDialog</literal> inherited If your widget inherits QDialog, it is probably intended to change one or more values; often this is used to set the application preferences. To invoke the dialog, you have to add a slot to the App class by adding the method declaration and the implementation body. Then add the constructor call to the method as well as a call to show() or exec() the dialog. Finally, you should take care for processing the results of the dialog; this can either be done by the dialog who changes values of the parent widget itself or by retrieving the values from the dialog (which would make your dialog a lot more reusable in other projects). Mind that you should call delete if you called the dialog instance with new to avoid memory leaks. Finally, you have to connect a menuentry (with according statusbar help) to the new slot invoking the dialog; optionally a keyboard acceleratoraccelerator and a toolbar toolbar icon. For this, add a resource ID to the file resource.h with a define. Then add an according menuentry to one of the popup menus already present in the menubar or create a new popup to add your menuentry. The menuentry consists of: an optional icon pixmap. Call this with the Icon("iconname.xpm") macro of KApplicationKApplication to use the provided KIconLoader instance. the menuentry name. Add this with the i18n("&&;entryname") macro of KApplicationKApplication to allow internationalization. The ampersand should be in front of the letter that will be displayed underlined to access the entry directly by keyboard acceleration. the member instance to call. Normally this would be the this pointer. the member slot to call. Use SLOT(yourmethod() to call the slot on the signal activated(). the acceleratoraccelerator key. This should be set to zero as this is done by an entry in initKeyAccel() where you have to introduce an acceleratoraccelerator key together with the slot to call. Then call changeMenuAccel() to change the menu item's acceleratoraccelerator. This will make it configurable by a key-chooser dialog later. For standard actions, use the enumerable values given by KAccelKAccel. the menu ID as set in resource.h Printing Support QPrinterQPainterQPrintdialogprinting Printing is usually provided by your application to let the user create a printed version of the document he created with the application; therefore only needed for those programs that are used to produce something the user may want to print out, e.g. text or pictures. In any case, this requires an interface that is provided by the Qt Qt library by two classes: the QPrintDialog class, offering the printing dialog, and the QPainter class that is also used to draw the widget's contents usually. As the view-class of an application is responsible for displaying a document, it also is responsible for printing. The Qt <indexterm remap="idx"><primary>Qt</primary></indexterm> Print Dialog The Qt Qt Printer dialog can be used including qprintdialog.h. When using the KDE framework application, this is already used by the view class, so you only have to complete the implementation of the method print() by using QPainter. The QPainter Class Independent of the printer's capabilities, you can use QPainter to draw your document onto the printer provided by QPrinter like you would when drawing onto a widget. The only difficulty would be where you have to implement the way things have to be printed. Help Functions A very important part of the development process is to provide help functionality to the user wherever possible. Most developers tend to delay this, but you should remember that a normal user isn't necessarily a Unix-expert. He may come from the the dark side of computer software usage offering all sweets that a user may need to work himself into using an application even without ever touching the manuals. Therefore, the KDE and Qt Qt library provide all means usually considered making an application professional in the eyes of the normal user by help functions that are ready to use. Within the application, those are: Tool-Tips Quick-Help Statusbar help What's this...? buttons Additionally, the application should provide means to access a HTML-based online manual directly using the standard help key F1. As KDevelop also offers all types of help as well as the KDE framework generated by the application wizard already contains support for this, this chapter will help you find out where and how to add your help functionality. During the development of your application you should try to be consistent whatever you're doing; therefore you should do the necessary steps directly while extending the code. This will prevent you from diving into the code again and figuring out what your application does or what you intended by certain parts of the code. Tool-Tips A very easy mean of providing help are tool-tips. Those are small help messages popping up while the user moves the mouse over a widget that provides a tool-tip and disappears when the mouse moves away. The most popular usage of tool-tips is made in toolbar toolbars where your tool-tips should be kept as small as possible because toolbar toolbars can be configured to display their contents in various ways: either displaying the button, button with text on the right, button with text below, text only. This possibility should be made configurable by the user, but isn't a must-be. The text is shown as a tool-tip anyway and a toolbar toolbar usually consists of buttons and other widgets like lineedits and combo boxes. For a complete reference, see the KToolBarKToolBar class reference located in the KDE-UI library. As an example, we have a look at the the "New File" button in a generic application: toolBar()->insertButton(Icon("filenew.xpm"), ID_FILE_NEW, true, i18n("New File") ); toolBar()KToolBar There, the part i18n("New File") provides a tool-tip message. It is enclosed by the i18n() macro provided by kapp.hkapp.h to translate the tool-tip towards the currently selected language. Tool-tips can also be added to any custom widget by using the classes QToolTip and QToolTipGroupQToolTipGroup provided by Qt Qt. An example of that would be: QToolTip::add( yourwidget, i18n("your Tip") ); For more information, see the Qt Qt-Online Reference, class QToolTipQToolTip. Adding Quick-help Quick-Help windows are another good example of providing help. The user can access the quick-help over a widget that it is connected to by pressing the right mousebutton and selecting "Quick-Help" in the context menu. Therefore, Quick-Help can be placed somewhere in between a detailed handbook reference help and tool-tips- the documentation would be too extensive and a tool-tip would not provide enough information. To see how Quick-Help works, open any dialog within KDevelop and press the right mouse button over a dialog item. Then select the Quick-Help menuentry and you're offered the help message. Additionally, those messages can be formatted by color, font and even can be used for containing URL's to refer a certain webpage (and therefore can refer to the documentation handbook as well). To make use of Quick-Help, add the include file kquickhelp.hkquickhelp.h to your sourcefile containing quick-help. As the KQuickHelpKQuickHelp class is part of the KDE-UI library, it should already be used by your application; if not, set the linker flags of your project to use kdeui. An example would be: KQuickHelp::add( yourwidget, i18n("your Tip") ); which is almost the same as with QToolTip. When constructing a dialog with the KDevelop dialogeditor, add your tool-tips and Quickhelp in the implementation file- NOT within the data sourcefile as this is rebuild by the dialogeditor every time you edit the widget. The KQuickHelpKQuickHelp class provides also formatting text by using tags. It allows hyperlinks including Internet protocols, colors, font types and sizes. See the KDE Library Reference Guide and the class documentation class documentation for KQuickTipKQuickTip for more information. Extending the Statusbar Help As the frame applications provided by KDevelop contain a statusbar as well, it also offers a set of statusbar messages already for all menu and toolbar toolbar items. A statusbar help message is a short message that extends the meaning of a tool-tip or can be seen as a replacement for a tool-tip over menubar items and is (as the name suggests) displayed in the statusbar when the user enters a menu and highlights the menu entry; therefore all menu items connect their signal highlighted(int) to the method statusCallback(int) which selects the according message in a switch statement. Whenever you add a menuitem to already existing menus or a toolbar toolbar item, add an according entry in this method with a short description of the action the user will cause when activating the button or menuentry. Example: case ID_FILE_NEW: slotStatusHelpMsg(i18n("Creates a new document")); break; This will display a statusbar message by calling the method slotStatusHelpMsg() with the according translated help string whenever the user highlights a menu or toolbar toolbar item with the id ID&_;FILE&_;NEW that is connected to the statusCallback() method. Toolbars connect to this method by their signal pressed(int), which allows the user to press the toolbar toolbar button and move away the mouse when he doesn't want to invoke the command. KToolBarKToolBar also offers the signal highlighted(int, bool) which can be used to display the message whenever the user highlights the button instead of the preset signal used. The "What's This...?" Button The "What's This...?" "What's This...?" button provides help windows like QuickhelpKQuickHelp, but with the intention that the user wants to get help about a certain widget within the working view or a toolbar toolbar item. It is placed in the toolbar toolbar and gets activated once the user hits the button. The cursor changes to an arrow cursor with a question mark like the button itself looks like. The the user can press on a visible widget item and gets a help window. As an exercise, you could try this behavior with the What's this...? button within KDevelop. To add the What's This...? button, do the following: include qwhatsthis.h into your sourcecode add a private member QWhatsThisQWhatsThis whats&_;this/ or with another member name to your KTMainWindowKTMainWindow derived class declaration define a resource id for your what's this button into the resource.h file,e.g. &#;define ID&_;HELP&_;WHATS&_;THIS 10100 in your method to create the toolbar toolbar (usually initToolBar()), add at the location you want to have the button displayed: whats_this = new QWhatsThis; QToolButton *btnwhat = whats_this->whatsThisButton(toolBar()); QToolTip::add(btnwhat, i18n("What's this...?")); toolBar()->insertWidget(ID_HELP_WHATS_THIS, btnwhat->sizeHint().width(), btnwhat); btnwhat->setFocusPolicy(QWidget::NoFocus); setFocusPolicy() finally, add the messages you want to have on a click over a certain widget like this: whats_this->add(class_tree, i18n("Class Viewer\n\n" "The class viewer shows all classes, methods and variables " "of the current project files and allows switching to declarations " "and implementations. The right button popup-menu allows more specialized " "functionality.")); Extending the Documentation with SGML <indexterm remap="idx"><primary>SGML</primary></indexterm> Due to the fact that projects often lack a complete set of user documentation, all KDevelop projects contain a pre-build handbook that can be easily adapted; therefore fulfilling another goal of KDE: providing enough online-help to support users that are not familiar with an application. This chapter therefore introduces you on how to extend the provided documentation template and what you have to do to make it available to the user. Why SGML ? SGML SGML (Standard Generalized Markup Language) itself is a language with which one can write specifications of a markup language, but not a markup language itself. The specification for that markup language is called a DTD (Document Type Definition) which contains the structure of a document and the valid tags to use. Then, an SGML system provides a set of replacement files that translate the DTD tags into the desired output - and this is the way it works. The most used output is probably HTML to provide online help through web-browsers in a time where Internet standards are available even on single-desktop systems. KDE makes extensive use of HTML documentation by it's KDEHelp KDEHelp application where all KDE applications KDE applications are listed and give access to their user manuals as well as by a helpmenu where the user can access the online-help directly from within the application. Now, KDE (and therefore KDevelop) use the SGML-Tools 1.x package (see \|\|), which was formerly known as the LinuxDoc package. It contains a DTD called LinuxDoc, and a set of mapping files for various output transformations and the necessary tools that actually do the replacement of LinuxDoc tags. The LinuxDoc DTD is based on the Qwertz DTD which itself was written to provide a good mapping (replacement of tags) especially for the &latex; text system, therefore is very usable to produce a good printed output. The package then got it's name from the usage for writing documentation for the LDP (Linux Documentation Project) and has only changed it's name due to the fact that it is an sgml-system that does not necessarily have a direct connection with the Linux project but can be used on any Unix-System; you can as well write your own DTD and mappings if you ever like to. In the meantime, another DTD as been made up to fit the same purpose: the "DocBook DTD". DocBook has obviously some advantages over the LinuxDoc DTD mostly in providing better tags and mappings for tables and the inclusion of graphics, but that is possible with LinuxDoc as well. The SGML-Tools therefore switched to provide support for the DocBook DTD in the 2.x version series, which also includes a converter to produce a DocBook sgml from a LinuxDoc master. The current state of KDE development is that we're still using the LinuxDoc DTD for some reasons: writing LinuxDoc documentation is easy installing the SGML-Tools 1.x for using LinuxDoc is even much easier KDE provides an additional tool called ksgml2html which adds the KDE documentation style to the output produced by the SGML-Tools' 1.x sgml2html converter to produce HTML output. I personally have encountered that while writing the KDevelop handbooks using the LinuxDoc DTD is very easy and lasts for the requirements I need for writing the documentation. The learning curve is very high, so you will be a sgml-tools/LinuxDoc DTD guru within days and that will save you a lot of time to work yourself into any formatting system such as &tex; for printed output for your documentation or a markup language for HTML output. One major reason for still using the sgml-tools 1.x is that most distributions ship with the package and all additional tools you need for other output formats. This makes the installation as easy as possible and the writing itself isn't very complicated as you will see. The output formats you can achieve witht the sgml-tools are: HTML output, with KDE look'n feel when using ksgml2html additionally plain text GNU info, Lyx format &tex;, DVI, PostScript and PDF format Rich Text Format (RTF) What the Documentation already contains When creating a KDevelop project, the subdirectory docs/en already contains the english index.sgml documentation file and the already produced output HTML files. Those are already included into the project as well as their installation destination is preset to the KDE HTML directory. The documentation is already adapted to your project name, version number and the programmer's information. Further, the output covers the index.html file containing the table of contents (which is opened by KDE Help when the user requests help); an installation introduction and a copyright information with regards to the GPL license. Therefore, when extending the documentation, you only have to add the information that is specifically to your project. Mind that for KDE projects you have to run "Make Doc-Handbook" from the "Project" menu again after the project is created. The index.sgml file is again processed by ksgml2html ksgml2html and the KDE-Style is added to the HTML output. Open the docs/en directory in the RFV and add the logotp3.gif file to the project by the context-menu; then set the file properties correctly to install the logo file into the same location the HTML files will go - to $(kde&_;htmldir)/en/&<;your&_;project&>;/logotp3.gif. Writing SGML Documentation This section has been added because SGML (or to be more precise: the LinuxDoc DTD) still seems to be difficult for beginners that want to write documentation. When looking at some KDE applications, I have seen some that contain an sgml file which is the template - but the author went over to edit the html output instead of the sgml file. This results in problems that translators have - if they want to provide documentation for your application in their native language, they will have to edit each file as well in HTML and this makes it impossible to reuse the documetation for other formats not only in the english version but all internationalized versions as well. You see that this is very short-thinking and a bad situation; personally I think that this results from the author's knowledge of HTML but not of SGML. As most will try to avoid learning a new formatting language, they will use the HTML output as the template that they edit. If you once find out how easy (and useful) SGML with LinuxDoc is, you will know that it's worth to learn a few more tags that built the SGML formatting. The following sections will therefore introduce you to the most important parts of an LinuxDoc sgml file and how to extend your documentation. The DTD Declaration An SGML file, whatever DTD it may use, always has to start with the DTD declaration. This tells the SGML parser about which DTD to use. Therefore, the first tag (a bracketed expression like &<;yourtag&>; your contents &<;/yourtag&>;) is always the DOCTYPE: &<;!doctype linuxdoc system&>; That tells your sgml formatter that it shall use the LinuxDoc DTD. The Document Structure Now, when using the LinuxDoc, the next tag is the start tag for the document style type. The LinuxDoc DTD offers a whole set of types that you can select from, dependent on the purpose of your document or it's legth. Available formats are: &<;notes&>; for short explanations &<;article&>; for writing articles with abt. 10-20 pages (suggested). This is used by the templates of KDevelop and most KDE applications. &<;report&>; for articles that are longer than an &<;article&>; type &<;book&>; for writing large books - the KDevelop handbooks have been written using this document type &<;slides&>; for slideshows. This is useful for presentations. You will use &latex; as output format in most cases, of course. &<;letter&>; for normal letters &<;telefax&>; for a telefax &<;manpage&>; for a manpage Mind that these are only describing how the document structure will look like in general - not the actual output. As mentionend, the KDevelop default generated template is using the &<;article&>; structure. This is used by most applications except KDevelop itself which uses the &<;book&>; format. In the HTML output this doesn't really matter much - but for &latex; e.g. this makes much difference. The handbooks are really "books" with separate pages for each chapter as the main difference. What follows is that the end of the sgml file must have an end-tag for the document structure type - for &<;article&>; this would be &&etago;;article&>;. Titlepages Now, after the document structure follows a section that describes all entries usually found on a titlepage. The predefined template doesn't use this explicitely but only sets the information for &<;title&>;, &<;author&>; and &<;date&>; as this lasts for most purposes. Especially when using a &<;book&>; structure, you probably want to define a complete titlepage. The following lists the according tags for this, taken from the sgml source of this handbook: &<;!doctype linuxdoc system&>; &<;book&>; &<;titlepag&>; &<;title&>;The KDevelop Programming Handbook &<;subtitle&>;The User Guide to C++ Application Design for the K Desktop Environment (KDE) with the KDevelop IDE, Version 1.0 &<;author&>; &<;name&>;Ralf Nolden &<;htmlurl url="mailto:Ralf.Nolden@post.rwth-aachen.de" name = "&<;Ralf.Nolden@post.rwth-aachen.de&>;"&>; &<;inst&>;The KDevelop Team &<;date&>;Version 2.1 , July 7, 1999 &<;abstract&>; This handbook itself is part of the KDevelop Integrated Development Environment and is therefore also licensed under the GNU General Public License; see &<;ref id="Copyright" name="Copyright"&>; for more information. &&etago;;abstract&>; This covers all a normal titlepage usually contains. The &<;author&>; tag can also include a &<;thanks&>; tag to insert some thanks to co-authors, lecturers etc. &<;inst&>; represents the institute or company for which the author wrote the documentation; you could also use your team-name here like I did. &<;abstract&>; now covers an short description that is placed on the titlepage as well. This is somewhat annoying on a printed version where this section would be printed on the back of the title page where the copyright notice etc. are collected; this can be changed for &latex; output when editing the &tex; file. Indices The LinuxDoc DTD defines a set of tags for various indices as they occur in usual documents. Those are: &<;toc&>; for the table of contents &<;lof&>; for the list of figures &<;lot&>; for the list of tables The according start-tags don't necessarily require an end-tag; they're inserted just after the titlepage before the actual beginning of the document with the according sections or chapters. Now, when it comes to indexing keywords for an index that is placed at the end of the document, you are provided four different tags; two which leave the indexed phrase within the page and two for index entries that are not displayed: &<;idx&>; for a normal index entry &<;cdx&>; for a true-type index entry &<;nidx&>; for an index entry not appearing in the text document &<;ncdx&>; as before for a tt-index entry These tags are ignored by all backends (the tool that does the mapping of the sgml-tags to their document format) except sgml2latex, which generates an index file index.idx that can be turned to a &tex;-index with makeindex index.idx. The index itself can be inserted into the &tex; output file afterwards with \printindex. I patched my mapping for the &latex; output to do this automatically (but still don't know how to include the index into the table of contents...). The Document Contents Now, after explaining most details of the general structure, we're coming to the actual document contents. Dependent on the document structure type, has to start with a &<;sect&>; tag, when using &<;book&>; you have to start with &<;chapt&>; for the chapters. After the start tag, you can structurize each chapter with &<;sect1&>;, &<;sect2&>; etc. up to the allowed level of sub-sections (4). The chapter start tag is followed by the chapter's title. There, you have the additional choice to use &<;title&>; and &&etago;;title&>; for the chapter's title (optional). Now, afte the title of the chapter, you have to add a &<;p&>; tag to actually start with the subsection's contents. Within that, you have almost all means to format your document with list, enumerations, itemizations and description lists. Further, quotations, code snippets and the like can be inserted by tags; see you sgmltools documentation guide for a complete list. What you should look for is the "special characters" section. This contains all valid replacements for characters that are different from the usual alphabet like brackets, slashes and symbols like trademark etc. With the given tags you can structurize your text contents as you application documentation requires. How to call Help in Dialogs help functions Calling help in dialogs is often done by adding a Help-button; then you add a slot that is called when the button gets pressed. Within the slot implementation, call kapp->invokeHTMLHelp( QString aFilename, QString aTopic ); where aFilename is the the filename to be called within your HTML documentation directory of the application; e.g index-3.html. aTopic then is the topic that is to be called. The hash prefix is automatically added; just enter the chapter you want to have on this page, actually this would be a subsection's name. Class Documentation with KDoc <indexterm remap="idx"><primary>KDoc</primary></indexterm> Another important part of the documentation is including a descriptive help for your class interfaces. This will allow you and other programmers to use your classes by reading the HTML class documentation class documentation that can be created with KDoc KDoc. KDevelop supports the use of KDoc KDoc completely by creating the KDE-library documentation, also your application frameworks are already documented. To work yourself into the provided code, it would be a good start to read the included documentation online. The following describes what to do to get the API API documentation, where KDevelop helps you add it and what kind of special tags KDoc KDoc provides additionally. How to use KDevelop's Documentation features To create the API API documentation after you generated a project, select "Make API API-Doc" from the "Project" menu. This will process all header files and create the HTML output. Then you can access the documentation by selecting "API API-Documentation" from the Help-menu or the according book symbol in the Documentation tree, folder "Current Project". The documentation is already cross-referenced to the KDE and Qt Qt online-class documentation class documentation, so you can follow the inheritance easily with the inheritance overview. This may help you getting started with the KDE and Qt Qt documentation as well. Adding Class and Member Documentation As KDevelop provides all means to add code automatically, it also offers direct documentation. Whenever you're using the Class Generator by choosing "Project"->"New Class", add a descriptive help message to the documentation field. This will add the documentation to the class header. When adding class member functions and attributes with the classtools classtools, add the member documentation to the according documentation fields as well. You may think that documentation is a part of the development process that isn't very necessary. But remember that the more your project grows and the more people take part on the development process, class documentation class documentation is the best help to save time. If developers have to guess by method names what exactly the method does, it is even more likely that the meaning is misunderstood and the method apparently doesn't do the job a developer guessed it would do. Therefore keep track of your documentation and rebuild it as often as possible. Besides this, the documentation files are NOT included into the project, nor do they have any internationalization support. Therefore all API API documentation should be held in English to allow international development groups to work with your sources. Whenever you may want to add documentation by hand into the header file, just add the documentation above the method or class in a C-comment style with the difference that the first line has to begin with a slash and a double asterisk. Example: /** enables menuentries/toolbar items */ void enableCommand(int id_); Special Tags NOTE: The following documentation of this chapter is taken from the KDoc KDoc documentation provided with KDoc KDoc by Sirtaj S. Kang taj@.kde.org), author of KDoc KDoc; Copyright (c) 1997 The documentation is a mixture of: Normal text. Paragraphs must be separated by at least one blank line. text of the form &<;pre&>; .....code fragments.... &<;/pre&>; Various tags of the form: @tagname [tag parameters] The valid tags for each type of source code entity are: Classes @short [one sentence of text] A short description of the class @author [one sentence of text] Class author @version [once sentence of text] Class version (I normally set this to the RCS/CVS tag "Id") @see [one or more references to classes or methods] References to other related documentation. Methods @see as above @return [one sentence] A sentence describing the return value @param [param name identifier] [param description] Describe a parameter. The param description can span multiple lines and will be terminated by a blank line, the end of the comment, or another param entry. For this reason, param entries should normally be the last part of the doc comment. Constants, Enums, Properties @see as above ALSO @ref As a departure from the javadoc format, the metatag "@ref" has the same format as @see, but can appear anywhere in the documentation (all other tags must appear on a line by themselves). Internationalization What is i18n ? i18n is an internationalization system that is used to offer internationalized versions of an application or project. The difficulty with writing applications is that they only support the language they originally are composed with; visually this can be seen on labels, menu entries and the like. Goal of the internationalization is to provide applications and library functions in the language of the user; therefore enabling users that are not capable of the original language to make use of the provided functionality and feel more comfortable. How KDE supports Internationalization KDE, as one of the most modern desktop environments, has set one of its numerous goals to provide applications for users in their native languages, and simplifies the work for developers to provide their application in any of the supported language. Technically, this is realized by the KDE File System KDE File System Standard which contains localization support for languages in terms of documentation and by providing application internationalization through the use of the KDE-core library class KLocale. This class does all the translation, dependent on the preferred language set in the KDE Control Center. The developer on the other hand only has to know two things to make his application able to use this feature: include kapp.hkapp.h into your sourcecode wherever a visible text appears in your application, e.g. in source files that contain QLabel QLabels. wherever you set the visual string, embrace it with the i18n() macro provided by kapp.hkapp.h to enable translation. whenever you have to access a locale object, use the klocale macro provided by kapp.hkapp.h That is almost all you have to watch for while coding. Mind that you should not internationalize any configuration strings that are used by KConfigKConfig, because this is not necessary on one hand and doesn't work for reading in values on the other. Adding a Language to your Project KDevelop also takes part on making life easier for developers to include native language support to their applications. Whenever you create a new KDE project, a po directory is added to the main project directory. There, your &<;application&>;.pot file will be placed after the generation is complete. The .pot file already contains all strings that are set up with the i18n() macro, therefore you only have to write your code using the macro again. From time to time, you should do a "Project"-"Make messages and merge", which will automatically extract all macros again and rebuilds the potfile. "Project"-menuPO-filestranslationsKLocale To add a language to your application, choose "Project"-"Add translation file", which opens the language selection dialog. Select the desired language and press OK. Then, the according &<;lang&>;.po file will be build in the po directory. Then start translating the po file by selecting it from the po directory in the Real File Viewer or from the LFV, folder "Translations". If you have KTranslator KTranslator installed, it will be opened in the "Tools" window with KTranslator KTranslator, otherwise as a text file in the header/resource window. KTranslator KTranslator makes it very easy to translate strings by scanning the existing translations of your local KDE installation, so they can be used already. For editing by hand, we'll have a look at an example: #: kscribble.cpp:619 msgid "Opens an existing document" msgstr "" The above shows a string that was extracted from the file kscribble.cpp at line 619. msgidmsgid and msgstrmsgstr are the tags which give the information for the translation; msgstrmsgstr will contain the translated string. There, you have to watch escape sequences such as \n or \t, which have to be included into the translation string. A German translation would therefore look like this: #: kscribble.cpp:619 msgid "Opens an existing document" msgstr "Öffnet ein existierendes Dokument" That would be all to watch for translation; after you're done, save the file. When make is run within the po directory, the message files will be processed and errors may occur if strings are not translated consistently, e.g. escape sequences are missing. Then edit the according message string again and make sure that make runs without errors. Additionally, you should be very careful when translating ampersands within text strings. The letters after ampersands are used as keyboard acceleratoraccelerators in conjunction with the ALT key to access menubar or popup menu items to change the keyboard focus keyboard focus to the selected item more quickly. Now, if the same acceleratoraccelerator letter appears in the same keyboard focus keyboard focus area (which would be the main widget on one time, and a dialog at another), each widget after the first one cannot be accessed by the supposed keyboard acceleratoraccelerator. So even translators have a responsibility for the usage of the application under their language. There is also no guarantee that the original letter will occur in the translation, so translators have to choose very carefully and should test the application under their language after they installed the translation to ensure it runs without these malfunctions. Translation Team Contacts The KDE Team also provides numerous contacts to developers that are contributing to the KDE project as translators. Those are organized in language teams and coordinate the translation work. For an actual list and information who to ask for translating your application, see \|\|. The information below is taken from the KDE web site and contains the current contact addresses as of March 06, 1999. If you want to join a team please write directly to one of the team coordinators. The translation of the KDE is organized by Juraj Bednar mailto:bednar@rak.isternet.sk and Matthias Elter me@kde.org You can subscribe KDE internationalization mailing list kde-i18n-doc@kde.org by sending a mail to kde-i18n-doc-request@kde.org with the word "subscribe" in the subject line. Before starting any translation work, please contact the according translation team leaders for coordination to avoid double work. br Breton translation team: team coordinators: Jañ-Mai DRAPIER jdrapier@club-internet.fr website: http://perso.club-internet.fr/jdrapier ca Catalan translation team: team coordinators: Sebastià Pla sastia@redestb.es cs Czech translation team: team coordinators: Miroslav Flídr flidr@kky.zcu.cz da Danish translation team: team coordinators: Erik Kjær Pedersen erik@binghamton.edu de German translation team: team coordinators: Thomas Diehl th.diehl@gmx.net website: \|\| mailing list: send a mail with 'subscribe' in the subject line to: kde-i18n-de-request@kde.org Webforum for discussions and user feedback: \|\| el Greek translation team: team coordinators: Theodore J. Soldatos theodore@eexi.gr eo Esperanto translation team: team coordinators: Wolfram Diestel diestel@rzaix340.rz-uni-leipzig.de es Spanish translation team: team coordinators: Boris Wesslowski, Alonso Lara Boris@Wesslowski.com website: \|\| mailing list: send a mail with 'subscribe' in the subject line to kde-es@kde.org et Estonian translation team: team coordinators: Hasso C. Tepper hasso@ewsound.estnet.ee fi Finnish translation team: team coordinators: Kim Enkovaara kim.enkovaara@iki.fi fr French translation team: team coordinators: Francois-Xavier Duranceau Francois-Xavier.Duranceau@loria.fr website: \|\| mailing list: send an empty mail to: kde-traduc-fr-subscribe@egroups.com he Hebrew translation team: team coordinators: Erez Nir erez-n@actcom.co.il hr Croatian translation team: team coordinators: Vladimir Vuksan vuksan@veus.hr hu Hungarian translation team: team coordinators: Marcell Lengyel miketkf@yahoo.com website:\|\| is Icelandic translation team: team coordinators: Logi Ragnarsson, logir@imf.au.dk Thorarinn R. Einarsson, thori@mindspring.com Bjarni R. Einarsson, bre@netverjar.is Hrafnkell Eiriksson, hkelle@rhi.hi.is Gudmundur Erlingsson, gudmuner@lexis.hi.is Richard Allen ra@hp.is it Italian translation team: team coordinators: Andrea Rizzi rizzi@kde.org ko Korean translation team: team coordinators: LinuxKorea Co. kde@linuxkorea.co.kr mk Macedonian translation team: team coordinators: Sasha Konecni sasha@msi-uk.com nl Dutch translation team: team coordinators: flidr@CyberGate.zcu.cz flidr@CyberGate.zcu.cz no Norwegian translation team: team coordinators: Hans Petter Bieker zerium@webindex.no pl Polish translation team: team coordinators: Piotr Roszatycki dexter@fnet.pl pt Portuguese translation team: team coordinators: Pedro Morais pmmm@camoes.rnl.ist.utl.pt pt&_;BR Brazil Portuguese translation team: team coordinators: Elvis Pfützenreuter epx@netville.com.br ro Romanian translation team: team coordinators: Paul Ionescu ipaul@romsys.ro ru Russian translation team: team coordinators: Denis Y. Pershin dyp@inetlab.com sk Slovak translation team: team coordinators: Juraj Bednar bednar@isternet.sk mailling list: send a mail with 'subscribe' in the subject line to: sk-i18n@rak.isternet.sk sl Slovenian translation team: team coordinators: blazzupancic@hotmail.com blazzupancic@hotmail.com sv Swedish translation team: team coordinators: Anders Widell d95-awi@nada.kth.se tr Turkish translation team: team coordinators: Gorkem Cetin gorkem@linux.org.tr zh&_;GB2312 Simplified Chinese translation team: team coordinators: Wang Jian larkw@263.net zh&_;TW&_;Big5 Chinese BIG5 translation team: team coordinators: Chou Yeh-Jyi ycchou@ccca.nctu.edu.tw Finding Errors debugging macros Debugging Macros provided by Qt <indexterm remap="idx"><primary>Qt</primary></indexterm> The Debugging Macros provided by the Qt Qt library can be read on the debug.html page of your Qt Qt Online Reference Documentation, acessable on the link "Debugging Techniques" at the Qt Qt Documentation index page. The most recently used macros are ASSERT(b) CHECK&_;PTR(p) Thereby, b is a boolean expression. Gives out a debugging warning if b is false; p is a pointer which is checked and gives out a warning, if p is null. Details can be found in the Qt Qt Online Reference. KDE Macros NOTE: This chapter is a copy of Kalle Dalheimer's kalle@kde.org explanation document about the KDEBUG KDEBUG macros included with the KDE libs package as kdebug.html Last modified: Sat Sep 13 11:56:01 CEST 1997 What is KDebug KDebug is a system of macros and functions that makes using diagnostic messages in your code more efficient. You can give a message one out of four severity level and an area. You can choose at runtime where diagnostic messages should go and which of them should be printed at all. How to use KDebug in your code The macro KDEBUG KDEBUG Using KDebug is very simple. All you have to do is to &#;include &<;kdebug.h&>; at the beginning of every source file in which you want to use diagnostic messages and output the messages by calling the macro KDEBUG KDEBUG. This macro expects three parameters. The first is the severity level. Use one of the following constants: KDEBUG&_;INFO KDEBUG&_;INFO KDEBUG&_;WARN KDEBUG&_;WARN KDEBUG&_;ERROR KDEBUG&_;ERROR KDEBUG&_;FATAL KDEBUG&_;FATAL The second parameter is the area. An area is a part of KDE that you define yourself. You can then at runtime choose from which areas diagnostic messages should be printed. Please see the file kdelibs/kdecore/kdebugareas.txt for a list of already allocated area ranges. Choose an area within the range allocated for your application. If your application is not yet in here and you have CVS access, you can allocate a range for your application here, otherwise just mail me. It is probably a good idea to define symbolic constants for the areas you want to use, but this is completely up to you. The third parameter, finally, is the text you want to output. KDebug automatically prepends the logical application name if you output to a file, to stderr or to syslog. A newline is always appended, you need not (and should not) use one yourself. If you need parameters, you can use one of the macros KDEBUG1, ..., KDEBUG9. These allow for one to nine additional arguments. The syntax is exactly the same as with printf, i.e. you have to include format specifiers in your message which get replaced by the additional parameters. An example: KDEBUG3( <idx/KDEBUG_INFO/, kmail_composer, "Message no. %d to %s has %d bytes", message_no, aMessage.to(), aMessage.length() ); KASSERT KASSERT There are also the macros KASSERT, KASSERT1, ..., KASSERT9 which work just like their KDEBUG KDEBUG-counterparts, except that they have an additional bool as their first parameter. Only if this evaluates to false will the message be output. Note: You should not use neither KDEBUG KDEBUG nor KASSERT KASSERT before the KApplicationKApplication object is constructed. Note 2: KDebug provides no means for internationalization because it is meant strictly for developers only. If you want to inform the user about an erroneous condition (like "this file is not writable"), use KMsgBox. Compiler switches You do not need any special compiler switches in order to use KDebug. But when you ship your product (this mainly applies to people who create distributions like .rpm or .deb packages), you should compile with the switch -DNDEBUG. This will simply remove all the debugging code from your application and make it smaller and faster (e.g. it uses 256K less non-shareable memory). How to manage diagnostic messages at runtime You can press Ctrl-Shift-F12 in every KApplicationKApplication at any time, and the "Debug Settings"-Dialog will appear. Here you can define separately for every severity level what should be done with the diagnostic messages of that level. The following settings are available: Output: In this Combobox, you can choose where the messages should be output. The choices are: "File", "Message Box", "Shell" (meaning stderr) and "syslog". Please do not direct fatal messages to syslog unless you are the system administrator yourself. The default is "Message Box". File: This is only meaningful when you have chosen "File" as the output and provides the name of that file (which is interpreted relatively to the current directory). The default is kdebug.dbg. Area: The areas which should only be output. Every message that is not mentioned here will simply not be output (unless this field remains empty which is the default and means that all messages should be output). Your can enter several areas separated by commas here, and you can also use area ranges with the syntax start-end. Thus a valid entry could be: 117,214-289,356-359,221. Please do not use whitespace. Apart from this, you can also tick the checkbox "Abort on fatal errors". In this case, if a diagnostic message with the severity level "KDEBUG&_;FATAL KDEBUG&_;FATAL" is output, the application aborts with a SIGABRT after outputting the message. When you close the dialog with OK, your entries apply immediately and saved in your application's configuration file. Please note that these settings are specific for one singular application! When you press cancel, your entries are discarded and the old ones are restored. The KDE File System Standard This chapter is a copy of the KDE-File System Standard as published on the KDE website at \|\|, written by Richard Moore rich@kde.org KDE File System Standard This file documents the directory structure that KDE and all KDE compliant applications should use. This is version 0.0.4 of the standard. Introduction The purpose of the KDE FSSTD KDE FSSTD is to ensure that all resources (icons, mimetypes etc.) needed for KDE applications KDE applications are stored in a consistent directory structure. Following this structure allows applications to make use of tools such as the KIconLoader class and allows separation of the platform specific data needed by KDE from platform independent data (making installations on multiple architectures possible). In this document directory names have been suffixed with a `/` character. Where the word 'appname' appears in angle brackets &<;like this&>; it means that there should be an entry corresponding to every installed KDE application. The word 'lang' is used in the same way to indicate that there should be an entry for every supported language named according to the standard two letter language codes eg. 'fr' for French, 'de' for German etc. Directory Layout The KDE directory structure is as shown below, the top of the KDE installation tree is usually '/opt/kde' and can be found at run time by using the kdedir() method of KApplicationKApplication (this replaces the KDEDIR environment variable the use of which is now deprecated). This document will refer to this directory as kdedir(). KDE File System.kdelnk files kdedir()/ bin/ Application binaries lib/ standard kde libraries (libkdecore etc.) &<;appname&>;/ Application specific data that is platform dependent include/ standard kde header files parts/ cgi-bin/ CGI programs for kdehelp share/ doc/ HTML/ default --> Link to kdedir()/share/doc/HTML/en &<;lang&>;/ &<;appname&>;/ index.html other application help files config/ applnk/ System/ Utilities/ Applications/ Games/ kfind.kdelnk .kdelnk khelp.kdelnk .kdelnk khome.kdelnk .kdelnk krefresh.kdelnk .kdelnk mimelnk/ magic text/ audio/ partlnk/ &<;partname&>;.kdelnk .kdelnk icons/ Icons used in kdelnk files &<;appname&>;.xpm mini/ Mini Icons for kpanel toolbar toolbar/ Standard toolbar toolbar pixmaps (eg. fileopen.xpm) wallpapers/ Wallpapers used by kdisplay apps/ &<;appname&>;/ toolbar toolbar/ Toolbar pixmaps pics/ Other application pixmaps application specific data (must be platform independent) &<;libname&>;/ pics/ locale/ &<;lang&>;/ LC&_;MESSAGES/ &<;appname&>;.mo What does this mean to application developers? A standard KDE application will install files into several places in the above structure. The only required items are the application binary, the application kdelnk file, the application icon and the application help files - all others are optional. The most common things that are installed are: Type of file Location Application binary (required) kdedir()/bin/ Application kdelnk file (required) kdedir()/share/applnk/ Application icon (required) kdedir()/share/icons/&<;appname&>;.xpm Application help files (required) kdedir()/share/doc/default/HTML/&<;appname&>;/&<;index&>;.html Application toolbar pixmaps kdedir()/share/apps/&<;appname&>;/toolbar/ Application platform independent data kdedir()/share/apps/&<;appname&>;/ Application platform specific data kdedir()/lib/&<;appname&>;/ KDE File System Application Documentation I've suggested making putting at least a single page in kdedir()/doc/default/HTML/&<;appname&>;/&<;appname&>;.html a requirement for KDE compliance. The application is free to use the directory to store any help data it requires. Applications that support more than one language would place the other languages inkdedir()/doc/&<;lang&>;/HTML/&<;appname&>;/&<;appname&>;.html with there being one 'lang' directory for each language code as usual. Arranging the files like this would allow links between the help files of two different applications that both support a given language. I am not 100&%; happy with the solution I've suggested as it does not allow any way to fall back to the default language if a required translation is not available. What does this mean to library developers? kdedir()/share/apps/&<;libname&>;/toolbar toolbar Toolbar icons for library widgets. kdedir()/share/apps/&<;libname&>;/pics Any other bitmaps for library widgets. File System Usage for KDevelop Projects As the last chapter covered the KDE File System Standard, this chapter deals with what you have to do to use the file system. A KDE project uses the file system at least for installation routines; therefore we will discuss setting installation properties for your project files. Your application may make use of files that are installed afterwards, where it is important to know how to get the relative pathname by the standard. This enables your application to work wherever the KDE file system may be and prevents hard-coding any file information. Accessing Files during Runtime After the installation of your project by end-users, your application may require file information during runtime. During the development process, you will experience at least one error which is caused when running your application within the KDevelop IDE and requiring the application manual by "Help"-"Contents" or pressing the F1 key. This will result in a message box, saying that the index.html file could not be found- if you haven't installed your application on your local KDE file system. Your application asks KDEHelp KDEHelp to open your index page with detecting the installation directory first through KApplicationKApplication's methods to access the file system, therefore, we will have a look at what KApplicationKApplication offers and make some example usage. Also other classes of KDE-Core make use of the KDE File System like KIconLoader and KLocale, which will be reviewed afterwards. <indexterm remap="cdx"><primary><literal>KApplication</literal></primary></indexterm>KApplication Methods The KApplicationKApplication class offers the following methods to access the KDE File System: void invokeHTMLHelp ( QString aFilename, QString aTopic ) const static const QString& kde_htmldir () static const QString& kde_appsdir () static const QString& kde_icondir () static const QString& kde_datadir () static const QString& kde_localedir () static const QString& kde_cgidir () static const QString& kde_sounddir () static const QString& kde_toolbardir () static const QString& kde_wallpaperdir () static const QString& kde_bindir () static const QString& kde_configdir () static const QString& kde_mimedir () static QString localkdedir () static QString localconfigdir () static QString findFile ( const char *file ) KDE File System The methods are generally used with the KApplicationKApplication object of your application, where KApplicationKApplication offers the macro kapp to receive the pointer: &#;define kapp KApplicationKApplication::getKApplication() Therefore, the methods are generally used like this: QString sounddir=kapp->kde_sounddir(); This example stores the path of the KDE sounddirectory under a QString, where you would append e.g. the sound filename. Then you can process this information and play a sound file that is located there. You should always test for the existence of a file by using QFileInfo's exists() method. Within these methods, void invokeHTMLHelp( QString aFilename, QString aTopic ) const [public] takes a special position to invoke the KDE help. Generally, you should use it everywhere a user needs to access information, e.g. when he is presented a modal dialog. The F1 key will not work to invoke the help contents, also the user should be presented the according help page. To make a good use of it, add a "Help" button to your dialog and create a slot that is used to connect on signal pressed(). In this method, use invokeHTMLHelp()invokeHTMLHelp() with the according page and subject; in case your application's documentation isn't written completely yet, leave this open to complete it after the documentation is in sync with the application. The documentation of KApplicationKApplication says: Invoke the kdehelp HTML help viewer. Parameters: aTopic This allows context-sensitive help. Its value will be appended to the filename, prefixed with a "&#;" (hash) character. aFilename: The filename that is to be loaded. Its location is computed automatically according to the KFSSTND. If aFilename is empty, the logical appname with .html appended to it is used. The methods of KApplicationKApplication will retrieve the following path's. kde_htmldir() kdedir()/share/doc/HTML Returns the directory where KDE stores its HTML documentation kde_appsdir() kdedir()/share/applnk Returns the directory where KDE applications store their .kdelnk file kde_icondir() kdedir()/share/icons Returns the directory where KDE icons are stored kde_datadir() kdedir()/share/apps Returns the directory where KDE applications store their specific data kde_localedir() kdedir()/share/locale Returns the directory where locale-specific information (like translated on-screen messages) are stored kde_cgidir() kdedir()/cgi-bin Returns the directory where cgi scripts are stored kde_sounddir() kdedir()/share/sounds Returns the directory where sound data are stored. This directory is for KDE specific sounds. Sound data of Applications should go into kde_datadir() kde_toolbardir() kdedir()/share/toolbar Returns the directory where toolbar icons are stored kde_wallpaperdir() kdedir()/share/wallpapers Returns the directory where KDE wallpaper files are stored kde_bindir() kdedir()/bin Returns the directory where KDE application binaries are stored kde_configdir() kdedir()/share/config Returns the directory where config files are stored kde_mimedir() kdedir()/share/mimelnk Returns the directory where mimetypes are stored localkdedir() $HOME/.kde Get the local KDE base dir localconfigdir() $HOME/.kde/share/config Get the local KDE config dir To search for a specific file, use findFile(const char *file) which will search several path's of the KDE File System: $KDEDIR, $KDEPATH, "&[;KDE Setup&];:Path=" entry in a config file. If the file is not found, the QString method isEmpty() will return True KIconLoader Methods QPixmap loadIcon ( const QString &&;name, int w = 0, int h = 0 ) QPixmap reloadIcon ( const QString &&;name, int w = 0, int h = 0) QPixmap loadMiniIcon ( const QString &&;name , int w = 0, int h = 0 ) QPixmap loadApplicationIcon ( const QString &&;name, int w = 0, int h = 0 ) QPixmap loadApplicationMiniIcon ( const QString &&;name, int w = 0, int h = 0 ) bool insertDirectory ( int index, const QString &&;dir&_;name ) Setting File Installation Properties As the above explained where KDE applications KDE applications should place their files and how to access them at runtime, the following will explain how to set the file properties correctly to ensure the files get installed at the right place. The Makefiles support a set of macros to install your files into the KDE File System and which have to be used for setting the file installation properties. To set the properties, open your project and select "Project"-"File Properties" which opens the File Properties dialog. The file properties are displayed if you select a filename currently included in the project. First of all, a file has a type property, which can be one of the following: HEADER: specifies a file as a header file SOURCE: specifies a file as a source file SCRIPT: specifies a file as a script file DATA: specifies a file as a data file that usually gets installed like pixmaps or HTML documentation files PO: specifies a file as a translation file KDEV&_;DIALOG: specifies a file as a dialog file to be interpreted by the dialog library Further, a file is included in the project, if "Include in Distribution" is checked. This ensures that the file is included in the distribution tarball or package. If a file has to be installed, you have to enable "Install". This will allow setting the Installation path for the selected file, where the filename is already inserted. Now, as said above, the Makefile already is capable of a set of macros for the KDE File System Standard. These are used to set the installation path and ensure that the files actually will land in the KDE file system and not somewhere else. Macros that can be used, have to be embraced in round brackets and are marked with the dollar sign in front of the macro. When configure builds the Makefiles on the end-user's system, it will determine values for these macros that match the real directory name and will expand the Makefile.am macro towards the actual destination. When looking a standard KDE application project, you will see on the file property of your index.html file that it already uses a macro to determine where it should go: $(kde&_;htmldir)/en/kscribble/index.html This says, that make should install the file index.html in the kde-html directory, subdirectory en for English, the application subdirectory and the filename. You could as well use another filename if you like to rename the file on the installation destination. For the destination of your binary you currently have to edit the project's Makefile.am if your destination should be different form the "Applications" section of kpanel: APPSDIR = $(kde&_;appsdir)/Applications Possible values are (as the KDE-File System Standard says): Applications Games Graphics Internet Multimedia Settings System Utilities Setting no directory will end your applnk directly in kpanel's root. The following list contains the macros that can be used in the installation setup for files: kde_htmldir Where your docs should go to. (contains lang subdirs) kde_appsdir Where your application file (.kdelnk) should go to. kde_icondir Where your icon should go to. kde_minidir Where your mini icon should go to. kde_datadir Where you install application data. (Use a subdir) kde_locale Where translation files should go to.(contains lang subdirs) kde_cgidir Where cgi-bin executables should go to. kde_confdir Where config files should go to. kde_mimedir Where mimetypes should go to. kde_toolbardir Where general toolbar icons should go to. kde_wallpaperdir Where general wallpapers should go to. Use these macros in conjunction with the according necessary subdirectories and the filename for setting the installation properties. By default, the currently created HTML documentation files, the kdelnk file, Icon, Miniicon and the translation files (also newly create ones) are already set up for their destination; therefore you don't have to make any changes for your default installation routine that has been set up by the application wizard of KDevelop. Organizing Project Data Another issue in creating projects often appears to the programmer if he has or wants to include additional data that have to be installed with the project. You already know where to install it, but what about organizing it in the source tree ? A good advice here may be to collect all data in directories that more or less match the KDE File System Standard, e.g. your application needs additional toolbar toolbar icons. Creating these icons in the main project directory is potentially not a good idea as they will be difficult to locate in the real file viewer and a removal will result in much work for each icon. Therefore, create your icon with "File"- "New" and choose a subdirectory toolbar toolbar; if it doesn't exist, it can be easily created with the "select directory" dialog. Existing icons can be copied and included into the project with "Project"-"Add existing file(s)", where you have to choose the files and the destination. When selecting the destination directory, you can create the toolbar toolbar subdirectory first within the selection dialog. After being finished, press OK and the files will be copied as well as included in the project. As an example, a toolbar toolbar icon should go to the following: $(kde&_;datadir)/&<;appname&>;/toolbar toolbar/&<;youricon&>;.xpm Pictures or additional icons that are not used as toolbar toolbar icons should go to a subdirectory pics instead of toolbar toolbar. The <literal remap="tt">kdelnk</literal> File The &<;appname&>;.kdelnk .kdelnk file currently included in your project will install itself in KDE's kpanel structure. You should think it is already created and complete, therefore shouldn't require any further notification. Despite of KDevelop's advanced qualities to help you with creating, programming and designing applications, it cannot determine the exact purpose of your application- and that is the information you have to add to the kdelnk file. As this is a text file, select it from the RFV or the LFV; it will be opened in the Header/Resource window. The sample kdelnk file would look like this: # KDE Config File &[;KDE Desktop Entry&]; Type=Application Exec=kscribble Icon=kscribble.xpm DocPath=kscribble/index.html Comment= Comment&[;de&];= Terminal=0 Name=kscribble Name&[;de&];=kscribble This already contains the basic configuration for the application specific data such as the icon, binary name, application name etc. You see that the section Comment is still empty. There you have to insert the Quick-Tip that will be displayed when the mouse cursor moves over the kdelnk file icon on the desktop or in kpanel. If scribble would be a small drawing program, you would enter e.g. Comment=A simple drawing program Each comment line afterwards will contain the same description translated in the language the brackets symbolize. Ask translators to insert a good translation in their native language or include the kdelnk file when asking for translating the application's po file; the same applies to the name of the application set in the Name lines. for more information about the purpose of the .kdelnk .kdelnk file, especially its use for commandline processing, see The KDE Library Reference Guide Programming Guidelines Close to the end of this handbook, I want to summarize several issues that programmers should watch out while coding. These are mostly C++ programming tips that relate to KDE and Qt programming especially and are taken from the KDE Developer's Center which can be found on the Internet at \|\|. Filenames First of all, when creating sourcefiles, you should always use lowercase filenames. KDevelop supports this guideline already if you stick to its filename aut-suggestion. This makes it easier for other developers to remember what source files to look for when they have to debug your application. Classnames The classnaming for KDE projects is recommended to be: class names should begin with a prefixed K followed by the name of the class by purpose (your choice). This would be e.g. KMyWidget for an application specific widget. the class members should always begin with lowercase letters, followed by uppercase beginnings for the next word, e.g. myWidgetPointer() methods that return a private members value shouldn't use the get-prefix. You should prefer using a descriptive name for those types of classmembers. Example: b&_;myboolean is a private member. The method returning the current value would be e.g. myBoolean(). File access within code Hardcoding any path should be avoided by using the KDE File System Standard. You only have to watch the installation path for your files by the according macros of the Makefile.am as described in this handbook. Within the code, you should use the methods of KApplication to retrieve the actual path. Class documentation Another thing already mentionend is class documentation. You should stick to use KDoc formatting rules as they are used by all KDE developers to document their classes. You should at least add a single line to all of your classmembers for yourself to remember the prurpose and for others to reuse code. The code-reuse by the GPL makes much more sense if you know where to find an already existing solution if classes are documented. The Qt library referece is a good example of well-documented interfaces, though it doesn't use KDoc. Use new to create widgets Within your implementation, you should always prefer to create widgets on the heap with new. The Qt library has a nice habbit to automatically delete all child widgets you created with new, so you don't ever have to use delete again in those cases. This is one of the most important practical features of the Qt library and you should make wide use of this. Debugging When it comes to debugging, you should make use of the macros KDebug provides. Those are similar to the Qt macros, but can be retrieved by the keycode STRG+ALT+F12. See the KDE Library Reference Guide for more information about the event filtering of these Macros. You could as well use assert(), but should try to be consistent with your debugging code. const-declarations Further, you should use const declarations for member functions that should or do not change any private member. This would be the case for all methods that only return the current value of a private member. This avoids changing the value accidently and will catch those logical errors at compile time. Now, towards initializing const members you should stick to do that together with using static in the declaration and initialize the value outside the constructor like this: class foo { static const int value; }; const foo::value = 10; ANSI C++ allows to initialize the member inside the constructor but you should avoid this as a few compilers are not capable of this feature. Virtual methods As explained in section User Interaction, you should stick to the access rights and the declaration by virtual when overwriting virtual methods. At least you shouldn't reduce the access of a virtual method from protected to private. Forward declarations Class-headers should be included where you dereference any object or instance of a class in your sourcecode. That means if your class uses a member of another class, replace the &#;include directive with a forward declaration of the class, e.g instead of: #include <qpushbutton.h> class KMyWidget:public QWidget { private: QPushButton* ok_button; }; you should prefer to only declaring the class QPushButton in the header file: class QPushButton; class KMyWidget:public QWidget { private: QPushButton* ok_button; }; and place the include directive into the according sourcefile where e.g. the instance ok&_;button is dereferenced with any method of the class QPushButton. This saves compile time at any rate, especially if you're using instances of classes that your're working on. The compiler will recompile all souces that include the header file if you made any changes on the interface of the class, therefore a simple addition of a method that only returns an internal value will lead to a recompilation of all sources tha include the header file of the class. Unused Parmeter Warnings and default arguments Also you should leave out formal parameters of methods that don't necessarily require the actual parameter to work. This avoids the unused parameter warnings of your compiler when he sees a method that retrieves a formal parameter but doesn't use it in its implementation. Usually, you will set some default arguments for several methods. Those should always be placed in the declaration of the class member instead of setting them in the member implementation. Using config.h KDevelop projects as well as any other project that is using autoconf to create a configure-script produce a file config.h after executing the configure-script on the target machine. The values found by configure are listed there and can be used within the sourcecode. The directive to include the config.h file is: #ifdef HAVE_CONFIG_H #include <config.h> #endif One of the most recently used entries of config.h is probably the type-definition of bool for compilers that don't comply with the newest ANSI C++ draft. Use 0 instead of NULL You should stickt to using 0 directly instead of NULL for preset values like the Qt and KDE libraries already do. This increases portablility of your applications towards different compilers that have problems with NULL. Temporaries You should declare temporary instances always before using them. This is generally considered better than direct use. Example: // Don't: for( int i=0; i<n; i++){ //do something }; // Do: int i; for(i=0; i<n; i++){ //do something }; This also counts on using temporaries in function calls: // Don't: setColor( &&;(QColor(black)) ); // Do: QColor color(black); setColor( &&;color ); References The KDevelop Programming Handbook contains information that are taken from various sources on the Internet and by mails to various mailing lists, as: KDoc KDoc documentation: Sirtaj S. Kang taj@.kde.org KDE Developer's Center maintained by Sirtaj S. Kang taj@.kde.org KDE Internationalization: Matthias Elter me@kde.org KDebug documentation: Kalle Dalheimer kalle@kde.org The KDE File System Standard: Richard Moore rich@kde.org KDE-Developer's mini-HOWTO: David Sweet <dsweet@chaos.umd.edu> The contents of the according chapters are copyright of the original authors. Copyright KDevelop Copyright 1998,1999 The KDevelop Team. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. Additional Information Example Makefile.am for a Shared Library # Example Makefile.am for a shared library. It makes a library # called "example" as libexample.so.2.1.2 # This Makefile.am was taken from the kdelibs distribution and modified # to serve as an example. # # David Sweet # INCLUDES= $(all_includes) lib_LTLIBRARIES = libexample.la # Note: If you specify a:b:c as the version in the next line, # the library that is made has version (a-c).c.b. In this # example, the version is 2.1.2. libexample_la_LDFLAGS = -version-info 3:2:1 $(all_libraries) include_HEADERS = header1.h header2.h\ header3.h # Which headers shouldn't be installed when a make install is done? noinst_HEADERS = version.h libexample_la_SOURCES = code1.cpp code2.cpp code3.cpp # USE_AUTOMOC is great. This takes care of all of your moc'ing # dependencies. # (You still need to include, for example, header1.moc in code1.cpp.) libexample_la_METASOURCES = USE_AUTOMOC